CS456
CS456

Reputation: 117

Recursively peek a map to get its values in Clojure

I'm trying to write a function which will recursively pop a map in order to get a value out, one at a time.

The following is what I've got so far..

(defrecord Stoptest [&args])

(def test (Stoptest. [:c101 :main-office :a1]))

(defn stopPop [x]
 (peek (-> x :&args))
 (recur(peek(rest x))))

(stopPop test)

I get an error saying the following:

clojure.lang.LazySeq cannot be cast to clojure.lang.IPersistentStack

What's causing this issue?

Cheers

Upvotes: 0

Views: 307

Answers (1)

Ivan Grishaev
Ivan Grishaev

Reputation: 1681

rest returns not a vector but a lazy seq. The error appears when you try to peek on in:

(peek (seq [1 2 3])) ;; gives the same error

The problem happens here because you have different type objects on each step of recursion. On the top, you have Stoptest instance. Next, you have a lazy sequence that behaves in other way.

I don't see any reason here to wrap your vector into a typed record. You can always iterate on the vector easily.

Upvotes: 1

Related Questions