Skip to content

Instantly share code, notes, and snippets.

@bhauman
Forked from pangloss/partition-chan.cljs
Created September 19, 2013 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhauman/6626683 to your computer and use it in GitHub Desktop.
Save bhauman/6626683 to your computer and use it in GitHub Desktop.
(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