0
votes

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 :)

1

1 Answers

2
votes

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))