Clojure # lambda marco doesn't always match (fn)?

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

Why is the second example error?

+4
source share
1 answer

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

You can try the following in REPL, which will show the exact extension:

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

All Articles