Skip to content

Instantly share code, notes, and snippets.

@jeaye

jeaye/reduce.clj Secret

Last active June 30, 2017 05:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeaye/829d0944c39de52e17214da25f08e307 to your computer and use it in GitHub Desktop.
Save jeaye/829d0944c39de52e17214da25f08e307 to your computer and use it in GitHub Desktop.
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