Skip to content

Instantly share code, notes, and snippets.

@aphyr
Created March 16, 2017 04:59
Show Gist options
  • Select an option

  • Save aphyr/66221a2233f2a8934c4f104d8ff193a1 to your computer and use it in GitHub Desktop.

Select an option

Save aphyr/66221a2233f2a8934c4f104d8ff193a1 to your computer and use it in GitHub Desktop.
(defn distinct-identical
"Like distinct, but only skips elements which are identical to those already
seen."
([xs] (distinct-identical xs {}))
([xs seen]
(when (seq xs)
(lazy-seq
(let [x (first xs)
h (hash x)
seen-xs (get seen h)]
(if (not-any? (partial identical? x) (get seen h))
(cons x (distinct-identical (next xs)
(assoc seen h (conj seen-xs x))))
(distinct-identical (next xs) seen)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment