Created
February 5, 2022 20:34
-
-
Save jackrr/b70ae84efffe052276c16266ac3f7eec to your computer and use it in GitHub Desktop.
Lazily remove duplicate members of list, by predicate "hashing" function on members
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-p | |
| ([pred coll] | |
| (distinct-p pred coll #{})) | |
| ([pred coll seen] | |
| (if (empty? coll) | |
| '() | |
| (lazy-seq | |
| (let [next (first coll) | |
| key (pred next) | |
| is-dup (contains? seen key) | |
| recurred (distinct-p pred (rest coll) (if is-dup seen (conj seen key)))] | |
| (if is-dup | |
| recurred | |
| (cons next recurred))))))) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: