1

Quite perplexed — if

(#(get % "a") {"a" "b"})

returns "b", why doesn’t

(-> {"a" "b"} #(get % "a"))

also return "b"?

(The second errors w/ CompilerException java.lang.ClassCastException: clojure.lang.PersistentArrayMap cannot be cast to clojure.lang.ISeq)

0

1 Answer 1

3

The thread first macro always places the arguments as the second item in the list. So in this case it is inserting it into the anonymous function definition instead of into a call to the anonymous function

(-> {"a" "b"} #(get % "a"))

expands into*:

#(get {"a" "b"} % "a")

which simply returns an anonymous function without running it. If you add another set of () it should work.

(-> {"a" "b"} (#(get % "a")))

will expand to:

(#(get % "a") {"a" "b"})

which places the arguments after the anonymous function in the outer list.

*I have expanded this by hand to show the idea more clearly. This is not the literal expansion (whch expands the reader macro and adds namespaces.

Sign up to request clarification or add additional context in comments.

3 Comments

In fact, (-> {"a" "b"} #(get % "a")) would be expanded to something like (fn* {"a" "b"} [p1__660#] (get p1__660# "a")) because #() is transformed to fn form at read time, i.e. before macro expansion.
This is what I refer to as "whch expands the reader macro" in the last sentence.
Sorry, I missed the sentence.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.