Fork me on GitHub
#specter
<
2020-01-30
>
joshkh17:01:33

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?

joshkh17:01:50

i suspect s/collect comes into play

nathanmarz18:01:50

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

nathanmarz18:01:59

(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)

nathanmarz18:01:06

that's one way to solve it

joshkh18:01:07

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