Fork me on GitHub
#specter
<
2017-02-28
>
ghadi20:02:15

is there a simple way to transform all values in a nested map recursively?

ghadi20:02:37

I'm trying to make a nicer printout of ElasticSearch mappings... here's a sample

ghadi20:02:15

{:properties {:foo {:type "blah" :properties {....}}

ghadi20:02:47

I'd like to collect all names along all paths... so that I end up with a list of ["blah" "foo"] ["blah" "bar"]

nathanmarz21:02:48

@ghadi you can use value collection for that

nathanmarz21:02:54

(def data
  {:foo {:type "blah"
         :properties {:foo {:type "bar"}
   :foo2 {:type "foo"}}}})

(def MY-WALKER
  (recursive-path [] p
    (continue-then-stay
      MAP-VALS
      (collect-one :type)
      :properties
      p
      )))

(select MY-WALKER data)
;; => [["blah" "bar" nil] ["blah" "foo" nil] ["blah" {:foo {:type "bar"}, :foo2 {:type "foo"}}] {:foo {:type "blah", :properties {:foo {:type "bar"}, :foo2 {:type "foo"}}}}]

nathanmarz21:02:34

for selection it returns each collected :type followed by the node value at that point

ghadi21:02:03

interesting... what does that return?

nathanmarz21:02:08

for transformation, your transform-fn will receive same thing but as arguments

ghadi21:02:29

thanks! I'm going to try to read the docs to understand that