Pankaj
Pankaj

Reputation: 312

Clojure reduce unable to cast Long to IFn

This weekend I decided to learn clojure. I am stuck with reduce.I get a class cast exception for

(reduce #((cond (= %1 0) %2 :else %1)) 0 '(1 1 1))

ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn  user/eval2092/fn--2093 (form-init1869535703328200664.clj:1)

I am not sure what exactly am I doing wrong. Any help is appreciated :)

Upvotes: 0

Views: 76

Answers (1)

d11wtq
d11wtq

Reputation: 35318

The function literal reader macro #( ... ) automatically provides the enclosing parentheses for the expression in the body, since it's usually a simple function application. In your case, the cond is being expanded to the numeric value 1, then the enclosing parentheses turn it into (1), which is not a valid function application.

Remove the inner parentheses:

(reduce #(cond (= %1 0) %2 :else %1) 0 '(1 1 1))

Upvotes: 2

Related Questions