Is there a quick way to create a `ReceiveChannel` ...
# coroutines
m
Is there a quick way to create a
ReceiveChannel
from an
Iterable
? à la
Copy code
fun <E> Iterable<E>.toChannel() =
	GlobalScope.produce { forEach { send(it) } }
m
Hey, I came up with this :
Copy code
fun <T> Iterable<T>.toChannel(): ReceiveChannel<T> {
    val channel = Channel<T>(Channel.UNLIMITED)
    GlobalScope.launch {
        this@toChannel.forEach { channel.send(it) }
        channel.close()
    }
    return channel
}
I only tested it on a quick example, seems to work ok !
m
Thanks! Is there a difference between the two?
d
I think
produce
might use a different type of channel. Other than that, produce vs. launch.