Re: communicating quits to timeouts AND parent loop in core.async

2013-07-17 Thread Ben Mabey
Ah, very nice. I had not seen the alt! macro in use but upon close look it is just what I need when using the :default option. My go-loop macro now only requires the single stop channel: (defmacro go-loop [bindings & body] (let [stop (first bindings)] `(let [~stop (chan)] (go (wh

Re: communicating quits to timeouts AND parent loop in core.async

2013-07-17 Thread Ben Mabey
This approach works for the simple case but gets cumbersome it a more realistic loop with several timeouts and conditional logic. For example, here is a reduced use case which I am using my go-loop macro I posted earlier: (let [heads? (fn [] (> (rand) 0.5)) heads-time-out 1 lo

Re: communicating quits to timeouts AND parent loop in core.async

2013-07-17 Thread Jonas
You can also use the alt! macro: (let [stop (chan)] (go (loop [] (println "Hello World!") (alt! (timeout 1000) (do (println "I'm done sleeping...") (recur)) stop :ok))) (!! stop :now) (println "We're done.")) On Wednesday, July

Re: communicating quits to timeouts AND parent loop in core.async

2013-07-17 Thread David Nolen
Why not the following? (let [stop (chan)] (go (loop [] (println "Hello World!") (let [[v c]] (alts! [(timeout 1) stop])] (if (= c stop) :done (do (println "I'm done sleeping, going to recur now...") (recur))) On Wed, Jul 1

communicating quits to timeouts AND parent loop in core.async

2013-07-17 Thread Ben Mabey
Hi all, In a number of places where I'm trying core.async I have run across the pattern of having a loop inside a go block with some timeouts in the loop body. Contrived example: (go (while true (println "Hello World!") (In situations like these I almost always want to