Created
October 12, 2012 17:27
-
-
Save b20n/3880381 to your computer and use it in GitHub Desktop.
This file contains 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 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))))) |
This file contains 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 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