This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-02-11
Channels
- # architecture (1)
- # babashka (61)
- # babashka-sci-dev (1)
- # beginners (85)
- # calva (112)
- # clj-kondo (279)
- # cljdoc (16)
- # cljs-dev (15)
- # cljsrn (7)
- # clojure (168)
- # clojure-europe (36)
- # clojure-nl (10)
- # clojure-spec (6)
- # clojure-uk (5)
- # clojured (1)
- # clojurescript (20)
- # core-async (16)
- # crypto (2)
- # cursive (13)
- # datomic (25)
- # events (7)
- # fulcro (21)
- # google-cloud (3)
- # graalvm (2)
- # graalvm-mobile (2)
- # gratitude (3)
- # helix (20)
- # honeysql (4)
- # hugsql (15)
- # introduce-yourself (15)
- # leiningen (2)
- # lsp (24)
- # luminus (22)
- # malli (21)
- # meander (11)
- # midje (1)
- # other-languages (1)
- # pathom (8)
- # re-frame (5)
- # reagent (5)
- # releases (2)
- # reveal (1)
- # shadow-cljs (18)
- # spacemacs (17)
- # sql (9)
- # tools-build (12)
- # tools-deps (4)
- # vim (12)
Hi everyone. Here is a modified example from https://clojuredocs.org/clojure.core.async/pub.
(def src (chan 1))
(def publisher (pub src :topic))
(def alice (chan 1))
(def bob (chan 1))
(def clyde (chan 1))
(sub publisher :celebrity-gossip alice)
(sub publisher :space-x bob)
(sub publisher :space-x clyde)
(def a-alice (atom []))
(def a-bob (atom []))
(def a-clyde (atom []))
(go-loop [heard (<! alice)] (reset! a-alice conj (str "alice heard: " heard)))
(go-loop [heard (<! bob)] (reset! a-bob (str "bob heard: " heard)))
(go-loop [heard (<! clyde)] (reset! a-clyde (str "clyde heard: " heard)))
(put! src {:topic :celebrity-gossip :data "omg she's prego!"})
(put! src {:topic :space-x :data "omg we're landing!"})
Why does @a-alice
return []?
@neil.barrett16 i haven't looked at this too carefully yet, but do you mean swap! instead of reset! for the atoms?
yes, sorry
Of course! I used conj
with reset!
.
Is there any way to get println
to work in a go
or go-loop
in Clojupyter? Does it work in a standard repl?
printing in background threads works with a standard REPL, but not always with nREPL-based things (Not sure what Clojupyter uses) standard REPL meaning clojure.main
Thanks ghadi.
Great! I'll try that.
How would I print from the log channel? Won't it have to be in a go-loop
?
Or would I write to a file?
This works with Clojupyter:
(def src (chan 1))
(def publisher (pub src :topic))
(def alice (chan 1))
(def bob (chan 1))
(def clyde (chan 1))
(sub publisher :celebrity-gossip alice)
(sub publisher :space-x bob)
(sub publisher :space-x clyde)
(def log-atom (atom nil))
(add-watch log-atom :key (fn [_ _ _ ns] (println ns)))
(defn log [msg]
(reset! log-atom msg))
(go-loop []
(when-let [heard (<! alice)]
(log (str "alice heard: " heard))
(recur)))
(go-loop []
(when-let [heard (<! bob)]
(log (str "bob heard: " heard))
(recur)))
(go-loop []
(when-let [heard (<! clyde)]
(log (str "clyde heard: " heard))
(recur)))
(put! src {:topic :celebrity-gossip :data "omg she's prego!"})
(put! src {:topic :space-x :data "omg we're landing!"})
(put! src {:topic :space-x :data "omg we're launching!"})
(close! src)