Forked from benashford/group_by_with_transducer.clj
Last active
April 5, 2016 16:22
-
-
Save jmilum/98e8f4e746bce0e421768e75b007c45c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment