We need parentheses here to make a call of anonymous function
user=> (-> [1 2 3 4] (conj 5) (#(map inc %)))
(2 3 4 5 6)
Why there is no need for parentheses around map+ and fmap+ in these examples?
user=> (def map+ #(map inc %))
#'user/map+
user=> (-> [1 2 3 4] (conj 5) map+)
(2 3 4 5 6)
user=> (defn fmap+ [xs] (map inc xs))
#'user/fmap+
(-> [1 2 3 4] (conj 5) fmap+)
(2 3 4 5 6)
The documentation for the
->and->>macros state that the forms after the first parameter are wrapped into lists if they are not lists already. So the question is why does this not work for#()and(fn ..)forms? The reason is that both forms are in list form at the time the macro expands.For example
gets the
(fn [x] ...)form at expansion time, so the macro thinks “great, it’s a list, I’ll just insert the 3 in the second position of the(fn ..)list.” Invoking macroexpansion, this is what we get:which of course doesn’t work. Similarly for
#():is expanded to
That’s why we need the extra parens.