【发布时间】:2016-11-23 20:09:35
【问题描述】:
我有这个数据结构:
(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)
但是当我这样做时,它要么超时,要么让我的电脑崩溃……我做错了什么?
【问题讨论】: