You have a local named rest
, bound by the function argument list. You're trying to call that rest
as if it were a function, rather than calling clojure.core/rest
.
I have a Clojure seq containing 3 functions. Why does (rest my-seq) give a "cannot be cast as" exception?
-
13-10-2022 - |
Pregunta
In the process of debugging a larger function, I created a simpler function to test where the error is:
(defn foo [a-val p1 p2 & rest]
(loop [curr-preds (cons p1 (cons p2 rest))]
(let [first-pred (first curr-preds)
first-bool (first-pred a-val)
second-bool ((second curr-preds) a-val)
third-bool ((last curr-preds) a-val)]
(println "\n\nLogical values: " first-bool second-bool third-bool)
(println "Is it a seq?" (seq? curr-preds))
(if (empty? curr-preds)
first-bool
#_(recur (rest curr-preds))
))))
p1, p2, and the collection of functions in rest are all predicates (e.g., odd?). I wrote this with the expectation that it would always be called with exactly 3 predicates.
When I take out the #_
on the next-to-last line, I get the following error:
java.lang.ClassCastException: clojure.lang.ArraySeq cannot be cast to clojure.lang.IFn
/Users/gr/temp/LTtemp1.clj:166 user/foo
RestFn.java:467 clojure.lang.RestFn.invoke
Through println
statements, I have found that:
curr-preds is a
seq
containing the 3 predicates, as expectedcalling each pred on
a-val
returns the expected resultcurr-preds
is, in fact, aseq
My question: rest
is defined to work on seqs, so why do I get the above cannot-be-cast error? Thanks.
Solución
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow