This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-10-09
Channels
- # 100-days-of-code (6)
- # announcements (4)
- # atlanta-clojurians (1)
- # aws (1)
- # beginners (65)
- # boot (21)
- # cider (9)
- # cljsrn (3)
- # clojure (186)
- # clojure-android (4)
- # clojure-conj (1)
- # clojure-dev (12)
- # clojure-germany (3)
- # clojure-italy (8)
- # clojure-nl (1)
- # clojure-russia (32)
- # clojure-spec (19)
- # clojure-uk (41)
- # clojurescript (83)
- # core-async (5)
- # cursive (18)
- # datomic (15)
- # emacs (5)
- # events (4)
- # fulcro (7)
- # hyperfiddle (4)
- # leiningen (4)
- # liberator (1)
- # off-topic (65)
- # overtone (4)
- # pedestal (5)
- # perun (1)
- # planck (3)
- # re-frame (1)
- # reagent (3)
- # rum (5)
- # shadow-cljs (8)
- # spacemacs (19)
- # testing (3)
- # tools-deps (4)
- # yada (6)
Hi, just stumbling on alt!
not working with a local var, and working with a vector literal. Is that expected? If so how do we deal with a dynamic list of channels to read from?
;; with local var
(let [stop-ch (async/chan 1)
ch1 (async/chan)
ch2 (async/chan)
chs [ch1 ch2]]
(async/go-loop []
(async/alt!
chs ([val ch]
(do
(println "got" val)
(recur)))
stop-ch (println "stopping loop")))
(async/put! ch1 "one")
(Thread/sleep 1000)
(async/put! ch2 "two")
(Thread/sleep 1000)
(async/put! stop-ch "stop"))
=> true
stopping loop
;; with vector literal
(let [stop-ch (async/chan 1)
ch1 (async/chan)
ch2 (async/chan)
chs [ch1 ch2]]
(async/go-loop []
(async/alt!
[ch1 ch2] ([val ch]
(do
(println "got" val)
(recur)))
stop-ch (println "stopping loop")))
(async/put! ch1 "one")
(Thread/sleep 1000)
(async/put! ch2 "two")
(Thread/sleep 1000)
(async/put! stop-ch "stop"))
got one
got two
=> true
stopping loop
Thanks @hiredman for the suggestion, the code below with alts!
works:
(let [stop-ch (async/chan 1)
ch1 (async/chan)
ch2 (async/chan)
chs [ch1 ch2]]
(async/go-loop []
(let [[val ch] (async/alts! (cons stop-ch chs) :priority true)]
(if (= stop-ch ch)
(println "stopping loop")
(do
(println "got" val)
(recur)))))
(async/put! ch1 "one")
(Thread/sleep 1000)
(async/put! ch2 "two")
(Thread/sleep 1000)
(async/put! stop-ch "stop"))
got one
got two
=> true
stopping loop