seb
12/18/2019, 7:48 AMoctylFractal
12/18/2019, 7:51 AMcount
works how you think it does: "The operation is terminal. This function consumes all elements of the original ReceiveChannel."atomic
counter, or having a separate coroutine that you explicitly send message through and that manages its own counterseb
12/18/2019, 7:53 AMoctylFractal
12/18/2019, 7:54 AMchannel.filter { x % 2 == 0 }.count()
seb
12/18/2019, 7:57 AMoctylFractal
12/18/2019, 7:58 AMseb
12/18/2019, 8:02 AMelizarov
12/18/2019, 8:50 AMjimn
12/18/2019, 8:59 AMtodo. FlowOn(done)
seb
12/19/2019, 2:50 AMoctylFractal
12/19/2019, 3:09 AMseb
12/19/2019, 3:13 AMelizarov
12/19/2019, 7:04 AMlen(chan)
to solve the problem you are facing. We did not add this ability to get the number of items in a channel because we did not see any use-case for it. We do have chan.isEmpty
, though (there were a couple of uses identified).seb
12/23/2019, 3:04 AMlen(chan)
to solve the problem you are facing.": just in my original example here in this thread.
A number of workers are processing items from a channel of tasks. While processing is ongoing, I want to know how many items are left.
In golang, I would launch one extra co-routine that would do
while (len(chan) > 0) log(len(chan))
Of course I can use an atomic counter to solve this, but that is like saying in order to know how much items are in a standard collection, I should count how many I put in...elizarov
12/23/2019, 7:15 AMlen(chan)
actually works. Can you write some small self-contained demo?seb
12/23/2019, 7:27 AMelizarov
12/23/2019, 8:30 AMlen(chane) > 0
. That is what Channel.isEmpty
does in Kotlin. I was under impression that you actually somehow use the result of len
other than comparing it with zero.for len(channel) > 0 { // line 44
item := <-channel // line 45
for
loop over the channel that works until the channel is closed. You need to close the channel at line 20.seb
12/23/2019, 8:36 AMfmt.Printf("%v items left\n", len(channel))
Isn't that a fair use case , to get to know how many items are left in a channel?elizarov
12/23/2019, 8:38 AMlen(chan) > 0
(isEmpty
) leads to easy-to-write-but-incorrect code. That was one of the reasons we’ve even considered removing isEmpty
, but …seb
12/23/2019, 8:42 AMelizarov
12/23/2019, 8:43 AMcount()
is deprecated and will be removed in the future.seb
12/23/2019, 8:43 AMelizarov
12/23/2019, 8:44 AMseb
12/23/2019, 8:52 AM