Drew Hamilton
01/20/2020, 4:18 PMinternal suspend fun <E> ReceiveChannel<E>.forEach(action: (E) -> Unit) {
val iterator = iterator()
while (iterator.hasNext())
action.invoke(iterator.next())
}
I was surprised that such an extension didn’t already exist—things like consumeEach
only consume existing items in the Channel and then cancel. Am I either missing a function that does what I want, or missing a reason that I shouldn’t do this?Dico
01/20/2020, 4:28 PMconsumeEach
is intended to be used as the sole consumer of a channel.
there is little wrong with for (item in channel){}
and your code is doing exactly that but without syntactic sugar.Dominaezzz
01/20/2020, 4:28 PMDrew Hamilton
01/20/2020, 6:34 PM