This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-12-22
Channels
- # adventofcode (78)
- # announcements (12)
- # babashka (2)
- # beginners (116)
- # calva (20)
- # cider (17)
- # clj-kondo (15)
- # cljs-dev (51)
- # clojure (32)
- # clojure-android (1)
- # clojure-dev (4)
- # clojure-europe (91)
- # clojure-gamedev (1)
- # clojure-italy (2)
- # clojure-nl (1)
- # clojure-spec (12)
- # clojure-taiwan (1)
- # clojure-uk (10)
- # clojurescript (9)
- # conjure (3)
- # cryogen (4)
- # cursive (4)
- # data-science (1)
- # datomic (5)
- # depstar (5)
- # fulcro (39)
- # google-cloud (2)
- # kaocha (2)
- # malli (7)
- # off-topic (3)
- # pathom (3)
- # pedestal (5)
- # re-frame (19)
- # rewrite-clj (54)
- # ring (3)
- # shadow-cljs (12)
- # spacemacs (12)
- # specter (3)
- # tools-deps (63)
Is there a way to use specter to traverse a nested map and return the key paths where a predicate on the value returns true?
for example, if the predicate is string?
and this is the nested map
{:a "foo"
:b {:c {:d "bar"}
:e 42}}
it should return
[[:a]
[:b :c :d]]
got it to work! the only part that’s missing now is to return :a
and :x
in a vector, like [[:a] [:x] [:b :c :d] [:b :f :y]]
(do
(defn paths-till-pred [v-pred data]
(let [walker (recursive-path [] p
(if-path map?
[ALL
(if-path [LAST v-pred]
FIRST
[(collect-one FIRST) ALL p])]))]
(select walker data)
))
(paths-till-pred string? {:a "foo"
:x "foo"
:b {:c {:d "bar"}
:e 42
:f {:y "baz"}}}))
=> [:a :x [:b :c :d] [:b :f :y]]