specter

Latest version: 1.1.3
2020-07-27T12:42:15.099600Z

Hi. I am trying to collect all values which are associated to a keyword :tags in a hierarchy of maps. Is there an easy way to do that using Specter?

2020-07-27T13:12:59.101Z

I tried this, but it does not walk into maps which have :tags:

(s/select [(s/walker #(and (map? %) (contains? % :tags))) :tags s/ALL]
          {:a {:tags [1 {:tags [2]}]}
           :b {:tags [3]}})
; => [1 {:tags [2]} 3]

nathanmarz 2020-07-27T18:53:22.102400Z

@vincent.cantin easy to do with recursive-path

user=> (def data
  #_=>   {:a {:tags [1 {:tags [2]}]}
  #_=>    :b {:tags [3]}})
#'user/data
user=> 

user=> (def MY-WALKER
  #_=>   (recursive-path [] p
  #_=>     (continue-then-stay
  #_=>       (cond-path map? MAP-VALS
  #_=>                  sequential? ALL)
  #_=>       p
  #_=>       )))
#'user/MY-WALKER
user=> 

user=> (select [MY-WALKER map? (must :tags) ALL] data)
[2 1 {:tags [2]} 3]