Created
September 14, 2013 14:19
-
-
Save pangloss/6562415 to your computer and use it in GitHub Desktop.
Prevent partition-chan from becoming a spin lock when the input channel is closed.
This file contains hidden or 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 partition-chan | |
([start-pred in] (partition-chan start-pred (complement start-pred) in)) | |
([start-pred end-pred in] | |
(let [out (chan)] | |
(go | |
(loop [] | |
(if-let [val (<! in)] | |
(if (start-pred val) | |
(let [next-chan (chan)] | |
(>! out next-chan) | |
(>! next-chan val) ;; capture the first message | |
(<! (tap-until end-pred in next-chan)) | |
(close! next-chan) | |
(recur))) | |
(close! out)))) | |
out))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Actually I think you want to recur in both cases when the start predicate is true or false.
https://gist.github.com/bhauman/6626683