1
我有这样的数据结构:为什么在Clojure中重复使用会导致崩溃?
(def initial-map {:name "Root"
:children [{:name "Child1" :children [{:name "Grandchild1"}]}
{:name "Child2"}
{:name "Child3"}]})
,我需要把它弄成这个样子:
[["Root" 0] ["Child1" 1] ["Child2" 1] ["Child3" 1] ["Grandchild1" 2]]
凡数字表示在数据结构中的节点的深度。
我已经写了这个功能,试图从第一到第二:
(defn node-xform [ret nodes depth]
(if empty? nodes)
ret
(recur (conj ret (map #(vector (:name %) depth) nodes))
(flatten (map #(:children %) nodes))
(inc depth)))
和我叫它这样
(node-xform [] (vector initial-map) 0)
但是,当我这样做,其超时或崩溃我的电脑...我做错了什么?