https://kotlinlang.org logo
#coroutines
Title
# coroutines
l

louiscad

07/05/2018, 8:17 AM
Hi, anyone knows why there's no
onEach
function for
ReceiveChannel
?
s

spand

07/05/2018, 8:57 AM
Cant you just iterate it?
Copy code
for (msg in channel) {
msg.stuff() 
}
l

louiscad

07/05/2018, 9:09 AM
I can, but it doesn't chain well with
filter
and other operations. I saw it would not be enough to satisfy my use case anyway, so I went with a simple
consumeEach
and additional collections for blacklisting part of the elements
w

withoutclass

07/05/2018, 2:56 PM
Be careful with
consumeEach
since it will close the channel if the coroutine you’re reading from exits/is canceled
The hard part of an
onEach
may be composability, for instance you’d probably need it at the start of the chain, because it’ll end up being something like iterating on the channel, doing some action, then returning the item
Or returning a new channel that you then need to iterate over as well
5 Views