At the Clojure REPL, this expression
( #(for [x %] (+ 100 (second x))) ['(+ 38) '(+ 48)] )
produces (138 148) as expected
but this
( #(for [x %] ((first x) 100 (second x))) ['(+ 38) '(+ 48)] )
produces (38 48) which seems truly weird.
Both expressions really should be producing the same result!
What am I missing?
Will appreciate any ideas to resolve this mystery.
BTW, I tried to use ‘apply (first x)’ and package the rest of the args into a list but it doesn’t seem to matter. The same unexpected result comes back.
Also, to verify that the + indeed gets resolved from the input, I gave the following to the REPL
( #(for [x %] (resolve (first x) )) '((+ 38) (+ 48)) )
which produced
(#'clojure.core/+ #'clojure.core/+) as expected.
In this the
+is a symbol, not a function, because it has been quoted in the list. However, symbols are defined as doing map lookup when invoked as a function (the same as keywords). So('+ 100 38)is the same as(get 100 '+ 38). That last argument is the “if you can’t find what I want in the map, return that”. Since100is not a map,+uses that argument as the return value.To make it do what you want you have two options:
Use vectors instead of quoted lists ensures that
+gets resolved appropriately.Resolve it yourself to ensure that you use the
+function instead of the+symbol.