Skip to content

Instantly share code, notes, and snippets.

@benashford
Last active January 14, 2017 17:34
Show Gist options
  • Save benashford/a9ea85225f4984174235 to your computer and use it in GitHub Desktop.
Save benashford/a9ea85225f4984174235 to your computer and use it in GitHub Desktop.
(defn group-by-with-transducer [key-f xf data]
(let [ff (memoize (fn [k] (xf conj)))
r (reduce (fn [m d]
(let [k (key-f d)
v (get m k [])]
(if (reduced? v)
m
(assoc m k ((ff k) v d)))))
{}
data)]
(into {}
(map (fn [[k v]] [k ((ff k) (unreduced v))]))
r)))
@sebastibe
Copy link

Line 4 I think it should key-f instead of first

@benashford
Copy link
Author

Yes, thanks for pointing it out, I've updated it now 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment