I can return a map from function with following syntax
(defn retmap [bar] { :foo bar })
How do I achieve the same with reader macro syntax? I tried following
(def retmap #({:foo %}))
But calling this function (retmap)
gives the error
clojure.lang.ArityException: Wrong number of args (0) passed to: PersistentArrayMap
You can use hash-map
:
(def retmap #(hash-map :foo %))
You can see why your example throws an exception by expanding the macro:
(macroexpand `#({:foo %}))
=> (fn* [x] ({:foo x}))
so the constructed map is immediately invoked as a function with no arguments. Maps are functions from keys to values so require an argument to be provided.