Skip to content

Instantly share code, notes, and snippets.

@b20n
Created October 12, 2012 17:27
Show Gist options
  • Save b20n/3880381 to your computer and use it in GitHub Desktop.
Save b20n/3880381 to your computer and use it in GitHub Desktop.
(defn my-window
"A version of streams/window that doesn't leak memory."
[n & children]
(let [window (atom (vec []))]
(fn [event]
(let [w (swap! window (fn [old]
(take-last n (conj old event))))]
(call-rescue w children)))))
(defn window
"A sliding window of the last few events. Calls children with a vector of the
last n events, from oldest to newest. Example:
(window 5 (combine folds/mean index))"
[n & children]
(let [window (atom (vec []))]
(fn [event]
(let [w (swap! window (fn [old]
(let [new (conj old event)
size (count new)]
(if (< n size)
(subvec new (- size n))
new))))]
(call-rescue w children)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment