5
如何搜索和分解多個後代密鑰。解析地圖的多個後代鍵?
例子:
(def d {:foo 123
:bar {
:baz 456
:bam {
:whiz 789}}})
(dissoc-descendents d [:foo :bam])
;->> {:bar {:baz 456}}
如何搜索和分解多個後代密鑰。解析地圖的多個後代鍵?
例子:
(def d {:foo 123
:bar {
:baz 456
:bam {
:whiz 789}}})
(dissoc-descendents d [:foo :bam])
;->> {:bar {:baz 456}}
clojure.walk在這種情況下非常有用:
(use 'clojure.walk)
(postwalk #(if (map? %) (dissoc % :foo :bam) %) d)
如果你想直接執行它,那麼我建議是這樣的:
(defn dissoc-descendents [coll descendents]
(let [descendents (if (set? descendents) descendents (set descendents))]
(if (associative? coll)
(reduce
(fn [m [k v]] (if (descendents k)
(dissoc m k)
(let [new-val (dissoc-descendents v descendents)]
(if (identical? new-val v) m (assoc m k new-val)))))
coll
coll)
coll)))
關於執行的關鍵事項:
人行道做深度優先遍歷。我想對於這種情況,廣度優先遍歷搜索的效率會更好,因爲如果要刪除父節點,那麼我們不需要遍歷其子節點 – Ankur 2012-03-16 04:54:43