Fork me on GitHub
#core-async
<
2019-04-29
>
Ivan Koz14:04:27

Is that possible to enclose >! <! into a function, and use that function inside a go block?

Ivan Koz14:04:00

most likely not, because function is a black box for state machine compiler, can somebody confirm?

Alex Miller (Clojure team)14:04:50

the parking calls need to be in the lexical scope of the go

Ivan Koz15:04:17

go block returns a channel, is that channel closed already? How to make multiple go blocks work with merge.

Ivan Koz15:04:05

at the end i want to group result of multiple concurrent tasks into a vector, so i was thinking of into merge few go's

bortexz15:04:23

The go block returns a channel that will emit the value returned by the body and then close, afaik

bortexz15:04:13

To do that, have a look at core.async/map, it applies f to the values read from the channels passed, to get a vector

bortexz15:04:34

(a/map vector go-block-chans-coll)

bortexz15:04:10

applies the function vector to all the values read, 1 value per channel, and puts into a vector

Ivan Koz15:04:04

yeah will do, surprised that go doesn't close channel after value was put into it

(<!! (async/map vector [(async/go 1)
                        (async/go 2)
                        (async/go 3)]))

👍 4
bortexz15:04:22

Doesn’t? I thought it does after putting the value

Ivan Koz15:04:59

strange why merge doesn't work properly then?

Ivan Koz15:04:14

(<!! (async/merge [(async/go 1)
                   (async/go 2)
                   (async/go 3)]))
=> 2

bortexz15:04:51

By merge you will have a channel that combines all the values from the others, so you will read one by one the results of the tasks as they come in

bortexz15:04:03

with map, it waits till all of them emit a value to execute f

Ivan Koz16:04:45

looks like in case of multiple go's merge will work aswell, it wasn't working at first, i was thinking go channels wasn't closed until you take a value

(<!! (async/map vector 
                [(async/go 1)
                 (async/go 2)
                 (async/go 3)]))
=> [1 2 3]
(<!! (async/into [] (async/merge [(async/go 1)
                                  (async/go 2)
                                  (async/go 3)])))
=> [1 2 3]

hiredman16:04:12

merge is not ordered