Skip to content

Instantly share code, notes, and snippets.

@Melipone
Created October 14, 2011 14:53
Show Gist options
  • Save Melipone/1287331 to your computer and use it in GitHub Desktop.
Save Melipone/1287331 to your computer and use it in GitHub Desktop.
remove-duplicates in clojure based on the distinct function
(defn remove-duplicates
"Returns a lazy sequence of the elements of coll with duplicates removed using a predicate"
[coll pred]
(let [step (fn step [xs seen]
(lazy-seq
((fn [[f :as xs] seen]
(when-let [s (seq xs)]
(if (some #(pred f %) seen)
(recur (rest s) seen)
(cons f (step (rest s) (conj seen f))))))
xs seen)))]
(step coll #{})))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment