开发者

Clojure's # lambda marco is not always the same as (fn)?

user> (map (fn [k] [k]) [1 2 3])
([1] [2] [3])
user> (map #([%1]) [1 2 3])
.... Error..

Why i开发者_如何学运维s the second example an error?


The #(<expr>) reader macro wraps the <expr> in an extra set of parenthesis, so #([%1]) expands to something equivalent to (fn [%1] ([%1])) and not (fn [%1] [%1]). So you are right. They are not entirely equivalent.

You can try the following in the REPL which will reveal the exact expansion:

user=> '#([%1])                   
(fn* [p1__862#] ([p1__862#]))
user=> '#(inc %1) 
(fn* [p1__865#] (inc p1__865#))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜