Fork me on GitHub
#core-async
<
2016-05-03
>
micahasmith18:05:25

so it looks like i shouldn’t do (while true in a (thread

micahasmith18:05:45

but is it safe to do a (thread (loop [data (<! chan)] either?

micahasmith18:05:15

given that a loop in (go is (go-loop i imagined that was a specialized version of (go

micahasmith18:05:10

hmm. well that is not the case:

(defmacro go-loop
  "Like (go (loop ...))"
  [bindings & body]
  `(go (loop ~bindings ~@body)))

hiredman19:05:13

I wouldn't put too much stock in that so answer

hiredman19:05:32

while is just a macro that expands to loop

micahasmith19:05:24

@hiredman: well thats good to know

micahasmith19:05:50

i can say that when i close the chan, even the thread chan, and i’m in a repl

micahasmith19:05:00

i still see prn lines outputting

micahasmith19:05:21

so it doesnt look to me like loop releases

micahasmith19:05:41

(`prn` lines within the while loop)

micahasmith19:05:11

but this doesnt make sense to me. wouldn’t this mean that even go-loops don’t stop looping?

micahasmith19:05:40

i mean, unless there is some special means via which bindings in go-loops are monitored, i would guess?

hiredman19:05:44

loop works the same way in or out of a go block or thread or future

hiredman19:05:03

if you recur in a loop, you loop

hiredman19:05:11

if you don't, you don't

hiredman19:05:48

the decision to use (while true ...) or not is: do you want to stop looping or not?

micahasmith19:05:20

i want to be able to stop yes

micahasmith19:05:36

man, now i’m wondering how go-loop ever stops looping

hiredman19:05:51

it is just like normalling use loop/recur

hiredman19:05:59

if you don't recur, you won't loop

micahasmith19:05:09

so basically only do it if you plan on never stopping the loop

hiredman19:05:45

are you familiar with the idea of a halting condition in a recursive function?

micahasmith19:05:21

base condition yes

hiredman19:05:39

that is how you stop looping

micahasmith19:05:05

but with a chan, it could resume, if its go-loop [data (<! my-chan)]

micahasmith19:05:26

like how do you base condition and then not base condition

hiredman19:05:29

no, if you don't recur, there will be no loop

hiredman19:05:51

if you have a base case, and you reach it, there is no loop

micahasmith19:05:56

so i use the fact that chans return nil when they’re done to avoid the recur?

hiredman19:05:12

nil means the channel is closed

hiredman19:05:20

or you can use anything you want

micahasmith19:05:28

everything makes sense!

hiredman19:05:30

there is no reason your base case has to be the channel is closed

micahasmith19:05:23

i always wondered why you can’t pub nil to a chan

micahasmith19:05:36

makes sense now