public
Created

  • Download Gist
test.clj
Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
(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])

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.