Skip to content

Instantly share code, notes, and snippets.

@Chouser
Created March 7, 2012 21:11
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 Chouser/1996279 to your computer and use it in GitHub Desktop.
Save Chouser/1996279 to your computer and use it in GitHub Desktop.
(defn stateful-filter [pred event-key cache events]
(lazy-seq
(when (seq events)
(let [event (first events)
ekey (event-key event)]
(if (pred (get cache ekey) event)
(cons event (stateful-filter pred event-key
(assoc cache ekey event) (rest events)))
(stateful-filter pred event-key cache (rest events)))))))
(stateful-filter
#(or (nil? %1)
(nil? %2)
(> (- (second %2) (second %1)) 10))
first
{}
'[[a 1] [b 2] [a 3] [b 5] [a 11] [a 12] [b 13] [a 14] [b 16]])
;=> ([a 1] [b 2] [a 12] [b 13])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment