Constructing a map on anonymous function in Clojure

I am just learning the language and I've got a simple question. Why does this work (constructs {:key "value"}):

(#(assoc {} :key %) "value")

But this doesn't:

(#({:key %}) "value")
ArityException Wrong number of args (0) passed to: PersistentArrayMap  clojure.lang.AFn.throwArity (AFn.java:429)

On Python the latter syntax is perfectly valid:

> (lambda v: {'key': v})('value')
{'key': 'value'}

edit: thanks for great answers, it is apparent I need to stop thinking # as equivalent to lambda in Python.


#(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.


That is the limitation of #() reader. fn will work fine.

user=> ((fn [x] {:key x}) "value")
{:key "value"}

Please take a look at the document Anonymous function literal (#())