Clojure offers tree-seq to traverse the depth of any value. This will simplify the logic needed to find your nested key:
(defn find-nested [mk] (->> (tree-seq map? vals m) (filter map?) (some k))) (find-nested {:a {:b {:c 1}, :d 2}} :c) ;; => 1
In addition, finding all matches becomes a replacement for some with keep :
(defn find-all-nested [mk] (->> (tree-seq map? vals m) (filter map?) (keep k))) (find-all-nested {:a {:b {:c 1}, :c 2}} :c) ;; => [2 1]
Please note that cards with nil values ββmay require special treatment.
Update:. If you look at the code above, you can see that k may actually be a function that offers much more features:
to find the string key:
(find-nested m #(get % "k"))
to find multiple keys:
(find-nested m
find only positive values ββin integer maps:
(find-nested m #(when (some-> % :k pos?) (:k %)))
xsc
source share