在 clojure 中,如何在嵌套地图中找到所有具有某些键的地图
In clojure, how to find all the maps in a nested map that have some key
设m
为嵌套地图。
如何在 m
中找到所有具有某个键的地图。
例如:
(def m {:a {:id 5}
:d {:id 58}
:x {:id 4 :c {:id 3 :d 4}}})
(recursive-filter m :id)
;; expected result:
> ({:id 5} {:id 58} {:id 4 :c {:id 3 :d 4}} {:id 3})
您可以使用以下内容:
(def data {:a {:id 5}
:d {:id 58}
:x {:id 4 :c {:id 3 :d 4}}})
(defn recursive-filter [m f]
(filter #(and (map? %) (f %))
(tree-seq map? vals m)))
(recursive-filter data :id)
灵感来自@Symfrog:
(defn recursive-filter [m k]
(filter #(and (map? %) (contains? % k)) (tree-seq map? vals m)))
设m
为嵌套地图。
如何在 m
中找到所有具有某个键的地图。
例如:
(def m {:a {:id 5}
:d {:id 58}
:x {:id 4 :c {:id 3 :d 4}}})
(recursive-filter m :id)
;; expected result:
> ({:id 5} {:id 58} {:id 4 :c {:id 3 :d 4}} {:id 3})
您可以使用以下内容:
(def data {:a {:id 5}
:d {:id 58}
:x {:id 4 :c {:id 3 :d 4}}})
(defn recursive-filter [m f]
(filter #(and (map? %) (f %))
(tree-seq map? vals m)))
(recursive-filter data :id)
灵感来自@Symfrog:
(defn recursive-filter [m k]
(filter #(and (map? %) (contains? % k)) (tree-seq map? vals m)))