Created
February 28, 2011 21:24
-
-
Save ray1729/848069 to your computer and use it in GitHub Desktop.
Example keep-only implementation to keep at most n elements in each bucket of a sequence
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 keep-only | |
[n bucket-fn s] | |
(letfn [(my-filter | |
[s seen] | |
(when (seq s) | |
(let [e (first s) | |
b (bucket-fn e) | |
c (inc (get seen b 0))] | |
(if (> c n) | |
(recur (rest s) seen) | |
(cons e (lazy-seq (my-filter (rest s) (assoc seen b c))))))))] | |
(my-filter s {}))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment