Constructing a map on anonymous function in Clojure

#(f %) is expanded by the reader into (fn [%] (f %). Likewise, #({:key %}) is expanded into (fn [%] ({:key %}). The python equivalent of this would be lambda v: {'key': v}(), which has the exact same problem as the Clojure version.

What you are looking for is something equivalent to (fn [v] {:key v}). If you really want to use #(...) notation, you could use #(do {:key %}).

Incidentally, I personally never use #(...). I think it’s more difficult to grok (as examples such as this evidence), and is only very slightly more compact than an equivalent fn form. Then there’s also the limitation that #(...) forms can not be nested.

Leave a Comment