specter

Latest version: 1.1.3
denik 2020-12-22T16:44:46.053600Z

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?

denik 2020-12-22T16:47:03.055500Z

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]]

denik 2020-12-22T18:10:40.057200Z

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]]