This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-01-24
Channels
- # arachne (3)
- # beginners (39)
- # boot (3)
- # cider (91)
- # cljs-dev (56)
- # cljsrn (4)
- # clojure (267)
- # clojure-dusseldorf (1)
- # clojure-estonia (1)
- # clojure-greece (2)
- # clojure-italy (6)
- # clojure-nl (2)
- # clojure-russia (18)
- # clojure-spec (27)
- # clojure-uk (136)
- # clojurescript (19)
- # core-async (2)
- # cursive (6)
- # datomic (17)
- # emacs (2)
- # fulcro (86)
- # graphql (4)
- # hoplon (13)
- # jobs-discuss (7)
- # jobs-rus (1)
- # keechma (34)
- # keyboards (7)
- # leiningen (5)
- # luminus (4)
- # lumo (8)
- # off-topic (13)
- # om (6)
- # onyx (26)
- # re-frame (22)
- # reagent (1)
- # reitit (2)
- # remote-jobs (8)
- # ring (3)
- # ring-swagger (5)
- # rum (8)
- # shadow-cljs (45)
- # specter (6)
- # unrepl (16)
- # yada (15)
I end up writing a lot of code that consumes a channel using a go loop. I've created a useful macro:
(defmacro go-while
"Accepts a single binding vector, and executes its body
so long as the channel that is bound produces a value.
The binding is standard binding vector of symbol and
a channel to read from (the source). The local symbol
will be bound to successive values taken from the source.
The source expression is evaluated only once.
Evaluates to a channel that is closed once the loop completes
(that is, when the source channel closes)."
[binding & body]
(let [[sym source] binding]
`(let [port# ~source]
(go-loop []
(when-some [~sym (<! port#)]
~@body
(recur))))))
(s/fdef go-while
:args (s/cat :bindings (s/and vector? ::specs/binding)
:body (s/* any?)))
This seems like a reasonable contribution to core.async, is it worth putting together a PR? If not, well, here it is, under ASL 2.0.@U04VDKC4G makes me think of Manifold's consume: http://aleph.io/codox/manifold/manifold.stream.html#var-consume.