简体   繁体   English

递归浏览地图以获取Clojure中的值

[英]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. rest不返回向量,而是一个懒惰的序列。 The error appears when you try to peek on in: 当您尝试peek时出现错误:

(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. 在顶部,您具有Stoptest实例。 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. 您总是可以轻松地在向量上进行迭代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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