Last active
May 15, 2023 22:28
-
-
Save briansunter/24cf3a357aaf2c4993cd6d6fd4c47980 to your computer and use it in GitHub Desktop.
distinct-by clojure transducer
This file contains hidden or 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 distinct-by | |
"Returns a lazy sequence of the elements of coll, removing duplicates of (f item). | |
Returns a stateful transducer when no collection is provided." | |
{:added "1.0"} | |
([f] | |
(fn [rf] | |
(let [seen (volatile! #{})] | |
(fn | |
([] (rf)) | |
([result] result) | |
([result input] | |
(let [value (f input)] | |
(if (contains? @seen value) | |
result | |
(do (vswap! seen conj value) | |
(rf result input)))))))) | |
([f coll] | |
(let [step (fn step [xs seen] | |
(lazy-seq | |
(when-let [s (seq xs)] | |
(let [h (first s) | |
t (rest s) | |
value (f h)] | |
(if (contains? seen value) | |
(recur t seen) | |
(cons h (step t (conj seen value)))))))] | |
(step coll #{})))) |
Here's simple version for anyone else who comes across this page.
(defn uniq-by
""
[f items]
(:uniq (reduce (fn [acc m]
(let [k (f m) ]
(if (get-in acc [:seen k])
acc
(-> acc
(update :seen assoc k true)
(update :uniq conj m)))))
{:seen {} :uniq []}
items)))
(comment
(uniq-by :name [{:name "Bob"} {:name "Jane"} {:name "Bob"}])
)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The excellent medley library has a correct (distinct-by f) transducer:
https://github.com/weavejester/medley/