Skip to content

Instantly share code, notes, and snippets.

@cljforge
Created December 22, 2015 10:48
Show Gist options
  • Save cljforge/eb46b7b21b2f9e5c5a39 to your computer and use it in GitHub Desktop.
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
(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