Fork me on GitHub
#specter
<
2017-10-19
>
kishanov03:10:36

Hi there. How do I do the following transformation with specter:

(let [data {:a {:val 15}
            :b {:val 18}}]

  (->> data
       (map (fn [[k v]]
              [k (assoc v :key (name k))]))
       (into {})))

kishanov03:10:28

i.e. is it possible to capture arguments in transform path and path them to transformation function?

nathanmarz04:10:22

@kishanov yes, with value collection

nathanmarz04:10:26

(transform [ALL (collect-one FIRST NAME) LAST :key]
  (fn [n _] n)
  data)

kishanov04:10:34

thnx @nathanmarz. Will the same approach work if I want to capture more then 1 value from the path? Ultimately I’m looking for the analog of “Capture groups” in instar

nathanmarz04:10:56

yes, you can capture as many values as you want

nathanmarz04:10:17

each collect/`collect-one` call produces an extra argument to the transform function, in the order in which they're called

kishanov04:10:38

nice, thanks!