Fork me on GitHub
#core-async
<
2018-09-16
>
danny00:09:30

Can you do (map <! chs) in the let binding?

seancorfield00:09:10

@clarkenciel I tried that but it doesn't seem to work either... since I was curious about @xiongtx’s question 🙂

seancorfield00:09:29

Ah, the following seems to work:

(defn consume-chs
[chs]
  (go-loop []
    (let [vsc (async/map vector chs) vs (<! vsc)]
      (when (every? some? vs)
        (println vs)
        (recur)))))

seancorfield00:09:32

Hmm, that seems to drop every other value put into the channels (so I'm probably doing something wrong).

danny16:09:17

This doesn't solve the skipping problem, but I think you could get ride of the vector argument in your example @seancorfield. given the original sample I think the channels have iterables on them. I reckon my take didn't work because the <! function doesn't do anything on its own and is primarily a bit of syntax that the go macro uses to figure out where to do thread parking. It looks like (async/map identity chs) would accomplish the same goal though.

seancorfield17:09:23

@clarkenciel No, (async/map identity chs) doesn't work -- I tried that first. What async/map does is apply the function across all the channels -- so you need a function that takes as many arguments as there are channels. identity only takes one argument so you'll get an arity error as soon as you have more than one channel in chs.

noisesmith17:09:30

maybe you want (take 1 (async/map vector chans)) - otherwise it would make a vector of every first item, vector of every second item, etc.

noisesmith17:09:58

in the original it was just one item from each chan

seancorfield17:09:15

async/map returns a channel containing the result of applying the function across all the channels.

noisesmith17:09:35

oh, right, so it's more complicated than that....

seancorfield17:09:42

Yeah, it's a weird operation.

seancorfield17:09:04

I spent quite a lot of time in the REPL with core.async last night trying to figure it out.

noisesmith17:09:19

(async/map vector (map (partial async/take 1) chans))

seancorfield17:09:04

I think you have a ) in the wrong place there 🙂

noisesmith17:09:44

yup, good eye

seancorfield18:09:24

It looked like @xiongtx wanted a vector of values back from a vector of channels -- hence the (every? some? vs) call...

seancorfield18:09:39

I just don't know why my code was dropping every other value (the values it did get were vectors of values from all the channels -- a vector of the first values followed by a vector of the third values, then fifth...

seancorfield18:09:21

...and then it stop when the first channel is closed.

noisesmith18:09:47

right, and my usage of take was trying to get the behavior of "one value from each chan"