Fork me on GitHub
#specter
<
2016-08-05
>
esirola06:08:30

hi, is it possible to implement something similar to a "group-by" operation with specter?

esirola06:08:31

i'd like to do something like

[{:a 1, :b 1} {:a 1, :b 2} {:a 2, :b 2}] -> [{:a 1, :b [1 2]} {:a 2, :b [2]}]

esirola06:08:42

... and I'm a bit puzzled

esirola06:08:00

you need to accumulate things while you traverse the collection somewhere, but I can't figure out how (if possible)

nathanmarz09:08:53

@jjcomer: will do once clojars comes back up

nathanmarz09:08:51

@esirola: I'm not sure how to express that with navigation

borkdude10:08:20

@esirola: I just implemented it as an exercise, not with Specter:

(let [data [{:a 1, :b 1} {:a 1, :b 2} {:a 2, :b 2}]]
       #_=>   (reduce (fn [acc [k v]] (conj acc {:a k :b (mapv :b v)})) [] (group-by :a data)))

borkdude15:08:42

What is the reason one path is called FIRST and another MAP-VALS, why not FIRST and SECOND?

nathanmarz15:08:02

@borkdude: MAP-VALS navigates to each value of a map, has nothing to do with second

borkdude15:08:56

@nathanmarz: ah, I mean [ALL FIRST] vs [MAP-VALS] - why is there no MAP-KEYS?

borkdude15:08:39

because this case is rare, probably

nathanmarz15:08:06

there could be a MAP-KEYS that's more efficient than ALL FIRST but I've never needed that

nathanmarz15:08:19

whereas MAP-VALS comes up all the time, so having an optimized version provides a lot of value

nathanmarz15:08:52

you can do (def MAP-KEYS (comp-paths ALL FIRST)) if you want something reusable

borkdude15:08:42

I was just wondering, didn't really need it

nathanmarz15:08:35

pretty much everything in Specter grew out of real use cases

borkdude15:08:50

I needed it when I wanted to solve a stackoverflow question though, I had to transform the keys