This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-07-06
Channels
- # admin-announcements (2)
- # boot (51)
- # bristol-clojurians (1)
- # capetown (14)
- # cider (4)
- # cljs-dev (3)
- # cljsrn (23)
- # clojure (76)
- # clojure-gamedev (2)
- # clojure-germany (2)
- # clojure-greece (2)
- # clojure-hk (5)
- # clojure-poland (1)
- # clojure-quebec (3)
- # clojure-russia (19)
- # clojure-spec (7)
- # clojure-sweden (10)
- # clojure-uk (77)
- # clojurescript (42)
- # clojurex (5)
- # core-async (40)
- # cursive (12)
- # datomic (58)
- # editors (1)
- # events (1)
- # heroku (1)
- # hoplon (4)
- # jobs (6)
- # jobs-discuss (1)
- # ldnclj (2)
- # lein-figwheel (1)
- # leiningen (5)
- # om (66)
- # onyx (51)
- # other-languages (80)
- # proton (20)
- # protorepl (12)
- # quil (3)
- # re-frame (3)
- # reagent (18)
- # spacemacs (14)
- # untangled (78)
- # yada (16)
Hi all! I created a really simple ping-pong simulation: https://gist.github.com/jmglov/8b89fe00aefe3f94f3b904eae9db32c1
But when I switch to <!
and >!
and change my loop
to go-loop
, my play
function simply returns immediately after printing "Ping" once.
@jmglov: go version of your code is non-blocking, that means the function with go-loop “spawns another thread” and returns immediately, (quotes used because in reality is it a bit more complex behind the scenes)
That makes sense, but shouldn't I still see pings and pongs printed from the spawned "thread"?
yes, if the process is still up and running, but if your -main
quits after calling the play function, core.async helper threads quit as well I think
Interesting. So >!
and <!
always need to be wrapped in a go
form, even when inside a go-loop
?
I’m not familiar with Clojure version of core.async, this is how I would write it in ClojureScript
That does work, but my timeout channels don't seem to have any effect, and I get this output:
Ping
Game over!
Pong
Ping
Pong
Ping
Pong
Ping
Pong
Ping
Pong
Ping
<!
must be inside go AFAIK, and “inside” means inside current code block, it does not transition across function call boundaries as you wrongly assume
And this one works perfectly:
(defn play []
(let [ch (async/chan 1)]
(async/go-loop [cnt 0]
(if (= cnt 0)
(do
(>! ch :ping)
(println "Ping")
(<! (async/timeout 500)))
(let [ball (<! ch)]
(if (= ball :ping)
(do
(>! ch :pong)
(println "Pong")
(<! (async/timeout 500)))
(do
(>! ch :ping)
(println "Ping")
(<! (async/timeout 500))))))
(if (< cnt 10)
(recur (inc cnt))
(println "Game over!")))))