Skip to content

Instantly share code, notes, and snippets.

@pangloss
Created September 14, 2013 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pangloss/6562415 to your computer and use it in GitHub Desktop.
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.
(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)))
@bhauman
Copy link

bhauman commented Sep 19, 2013

Actually I think you want to recur in both cases when the start predicate is true or false.

https://gist.github.com/bhauman/6626683

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment