Created
December 22, 2015 10:48
-
-
Save cljforge/eb46b7b21b2f9e5c5a39 to your computer and use it in GitHub Desktop.
Returns a lazy sequence of the elements of coll, removing any elements that return duplicate values when passed to a function f
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 any elements that | |
return duplicate values when passed to a function f." | |
[f coll] | |
(let [step (fn step [xs seen] | |
(lazy-seq | |
((fn [[x :as xs] seen] | |
(when-let [s (seq xs)] | |
(let [fx (f x)] | |
(if (contains? seen fx) | |
(recur (rest s) seen) | |
(cons x (step (rest s) (conj seen fx))))))) | |
xs seen)))] | |
(step coll #{}))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment