specter

Latest version: 1.1.3
joshkh 2020-01-30T17:39:33.015Z

is this an okay place to ask for help with an example? i have a simple tree, and i would like to collect a value from each parent and combine it with a value on its direct descendants. for example:

(def tree {:id       1
           :value    "one"
           :children [{:id    2
                       :value "two"}
                      {:id       3
                       :value    "three"
                       :children [{:id    4
                                   :value "four"}]}]})
where after a transformation, each child gets a :parent+myvalue key which is equal to (str (:value parent) (:value self))
(s/transform [...path] xfn tree)

{:id       1
 :value    "one"
 :children [{:id             2
             :value          "two"
             :parent+myvalue "onetwo"}
            {:id             3
             :value          "three"
             :parent+myvalue "onethree"
             :children       [{:id             4
                               :value          "four"
                               :parent+myvalue "threefour"}]}]}
is Specter well suited for this, or should i just use core walk functions?

joshkh 2020-01-30T17:41:50.015500Z

i suspect s/collect comes into play

nathanmarz 2020-01-30T18:12:50.016Z

@joshkh this is the kind of stuff specter is very good at

nathanmarz 2020-01-30T18:12:59.016300Z

(def CHILDREN (path (must :children) ALL))
(def VALUE+CHILDREN (path (collect-one :value) CHILDREN))

(def WALKER
  (recursive-path [] p
    (continue-then-stay
      DISPENSE
      VALUE+CHILDREN
      )))

(transform [VALUE+CHILDREN WALKER (collect-one :value) :parent+myvalue]
  (fn [parent-value my-value _]
    (str parent-value my-value))
  tree)

nathanmarz 2020-01-30T18:13:06.016500Z

that's one way to solve it

joshkh 2020-01-30T18:15:07.017700Z

aha, thanks for the working example. i always come back to specter after just long enough away to forget its paradigms 🙂