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

Luis Munoz

03/06/2020, 10:02 PM
For a channel what is the opposite of a flatMap, in other words how to take a channel of some array and start sending the values individually?
b

bdawg.io

03/06/2020, 10:07 PM
Are you saying
Channel<Array<T>>
and flatten it to
Channel<T>
?
l

Luis Munoz

03/06/2020, 10:10 PM
yes exactly
d

Dennis

03/06/2020, 10:11 PM
I edited the example from https://kotlinlang.org/docs/reference/coroutines/channels.html#pipelines
Copy code
fun <T> CoroutineScope.flatten(channel: ReceiveChannel<Array<T>>): ReceiveChannel<T> = produce {
    for (items in channel) {
        for(item in items) send(item)
    }
}
However there could be something already that i don't know...
l

Luis Munoz

03/06/2020, 10:13 PM
cool thanks!
b

bdawg.io

03/06/2020, 10:16 PM
If you're going to do other operators, you should consider using a
Flow
Copy code
val myChannel = channel
    .consumeAsFlow()
    .flatMapConcat { it.asFlow() }
    .filter { ... }
    .map { ... }
    .produceIn(myScope)
d

Dennis

03/06/2020, 10:18 PM
5 Views