This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-10-19
Channels
- # beginners (37)
- # boot (96)
- # cider (23)
- # clara (2)
- # cljs-dev (40)
- # clojars (1)
- # clojure (149)
- # clojure-conj (1)
- # clojure-dev (2)
- # clojure-dusseldorf (5)
- # clojure-france (82)
- # clojure-italy (1)
- # clojure-nlp (1)
- # clojure-russia (13)
- # clojure-spec (24)
- # clojure-uk (62)
- # clojurescript (131)
- # core-async (13)
- # core-logic (7)
- # data-science (1)
- # datomic (10)
- # defnpodcast (3)
- # docker (4)
- # emacs (3)
- # events (4)
- # hoplon (68)
- # klipse (4)
- # leiningen (1)
- # off-topic (5)
- # om (140)
- # onyx (16)
- # pedestal (24)
- # planck (10)
- # proton (2)
- # re-frame (9)
- # reagent (4)
- # remote-jobs (1)
- # ring-swagger (16)
- # untangled (5)
- # vim (8)
- # yada (30)
I have a question about stopping go-loop
which has alts!
inside.
Normally for go-loop
which has <!
inside, I do this
(def c (chan))
(go-loop []
(let [m (<! c)]
(when m
(do-stuff m)
(recur))))
(close! c)
But for one with alts!
inside, I have to do this.
(def c1 (chan))
(def c2 (chan))
(go-loop [closed #{}]
(when-not (= (count closed) 2)
(let [[m c] (alts! [c1 c2])]
(if (not= m :close)
(do (do-stuff m)
(recur closed))
(recur (conj closed c))))))
(go (>! c1 :close))
(go (>! c2 :close))
Is there a less verbose way to achieve this?you can change closed to be opened, and alts on it, and remove channels when they are closed, and exit the go loop when opened is empty
(def c1 (chan))
(def c2 (chan))
(go-loop [open [c1 c2]]
(let [[m c] (alts! open)]
(if m
(do
(do-stuff m)
(recur open))
(recur (remove #{c} open)))))
(go (close! c1))
(go (close! c2))
The problem I found with this implementation is that alts!
will always success with nil
from that closed channel and it will keep looping for free until the other channel is closed.