Skip to content

Instantly share code, notes, and snippets.

@ray1729
Created February 28, 2011 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ray1729/848069 to your computer and use it in GitHub Desktop.
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
(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