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)
但是,當我這樣做,其超時或崩潰我的電腦...我做錯了什麼?