Skip to content

Instantly share code, notes, and snippets.

@raek
Created October 14, 2011 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raek/1287265 to your computer and use it in GitHub Desktop.
Save raek/1287265 to your computer and use it in GitHub Desktop.
(defn distinct-key
"Returns a lazy sequence of the elements of coll with (k element) duplicates removed"
[k coll]
(let [step (fn step [xs seen]
(lazy-seq
((fn [[f :as xs] seen]
(when-let [s (seq xs)]
(let [key (k f)]
(if (contains? seen key)
(recur (rest s) seen)
(cons f (step (rest s) (conj seen key)))))))
xs seen)))]
(step coll #{})))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment