Create a gist now

Instantly share code, notes, and snippets.

@jeaye /reduce.clj Secret
Last active Jun 30, 2017

What would you like to do?
user=> (def data [{:a 0 :data :foo} {:a 1 :data :bar} {:a 0 :data :spam} {:a 2 :data :meow}])
#'user/data
user=> (def grouped (reduce (fn [acc o] (update acc (:a o) #((fnil conj []) % (:data o)))) {} data))
#'user/grouped
user=> grouped
{0 [:foo :spam] 1 [:bar] 2 [:meow]}
user=> (map (fn [[k v]] {:a k :items v}) grouped)
({:a 0 :items [:foo :spam]} {:a 1 :items [:bar]} {:a 2 :items [:meow]})
user=>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment