user=> ((fn [x] x) 5)
5
user=> (#(%) 5)
Execution error (ClassCastException) at user/eval1576$fn (REPL:1).
class java.lang.Long cannot be cast to class clojure.lang.IFn (java.lang.Long is in module java.base of loader 'bootstrap'; clojure.lang.IFn is in unnamed module of loader 'bootstrap')
I'd expected the two anonymous funtions to be equivalent. I kind of get the error in the second case: the evaluation of the anonymous function yields (5)
, which produces the exact same error.
However, why am I not getting this error in the first case? What's the difference?
#(...)
, when there's only %
inside and not other things that signify more than one argument, is equivalent to (fn [%] (...))
. Not to (fn [%] ...)
, which you expected.
So #(%)
is the same as (fn [%] (%))
- it's a function that calls its argument as a function without arguments.