Fork me on GitHub
#core-async
<
2015-07-28
>
danielcompton05:07:58

I need to take one value from each channel in a collection, but the ordering doesn’t matter. Each time I get a value from a channel, I want to remove that channel from the list I’m waiting on. Does anyone have a good example of this pattern?

danielcompton05:07:28

I’ve done

(loop [chans mychans]
      (let [[_ p] (async/alts!! chans)
            new-chans (remove #(= p %) chans)]
        (when (not-empty new-chans)
          (recur new-chans))))

danielcompton05:07:07

but it feels a little inelegant, and I need to guard against calling alts!! with an empty collection of chans

jcf10:07:52

Is anyone using offer! in production? Looks like it’s documented but hasn’t been shipped to Maven.

akiel11:07:08

@danielcompton: I do the same. The O(n) remove is not nice but alts! needs a vector so a set doesn't work.

danielcompton11:07:55

It’s O(n^2) overall

akiel11:07:21

in case alts!! is only O(n)

jcf12:07:21

@danielcompton: not sure this will work but have you tried using mix to combine the channels into one, and then when you read use unmix to remove that channel from the mix?

danielcompton12:07:55

That could work...