简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM