I have a Clojure seq containing 3 functions. Why does (rest my-seq) give a "cannot be cast as" exception?

StackOverflow https://stackoverflow.com/questions/21870027

문제

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 expected

  • calling each pred on a-val returns the expected result

  • curr-preds is, in fact, a seq

My question: rest is defined to work on seqs, so why do I get the above cannot-be-cast error? Thanks.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top