Fork me on GitHub
#specter
<
2017-08-30
>
lvh19:08:57

I have a data structure like:

{:x
 {:a {:y 1}
  :b {:y 2}
  :c {:y 3}}}
And I’d like {:a 1 :b 2 :c 3}. I can write the select path that gets the values: [:x (sr/multi-path :a :b :c) :y], and I guess I can zipmap from there; but is there an elegant way to get specter to do that for me?

lvh19:08:51

sr/collect’ing the multi-path doesn’t really do what I expected: [[[{:y 1} {:y 2} {:y 3}] nil]]

nathanmarz19:08:21

@lvh something like this? (select-any [:x (transformed MAP-VALS :y)] data)

lvh19:08:18

Cool; thanks! I guess my sample was a little underspecified; there are other keys in there I do not want; but I guess a multi-path instead of a MAP-VALS would probably do the trick; lemme try

nathanmarz19:08:36

for that you will want multi-transform with keys you don't want becoming (terminal-val NONE) and ones you do using (terminal :y)

nathanmarz19:08:14

if you want to do it in a one-liner that is

nathanmarz19:08:33

can also just do the select-any followed by a (setval [MAP-KEYS #{:k1 :k2 ...}] NONE data2)

nathanmarz19:08:06

I would just do the latter since there's no multi-transformed

nathanmarz19:08:29

actually, just use submap before transformed

nathanmarz19:08:35

that's the easiest and most efficient

nathanmarz19:08:28

[:x (submap [:k1 :k2...]) (transformed MAP-VALS :y)