parMapUnordered
@FlowPreview
@ExperimentalCoroutinesApi
Like map, but will evaluate effects in parallel, emitting the results downstream. The number of concurrent effects is limited by concurrency.
See parMap if retaining the original order of the stream is required.
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.flow.collect
import arrow.fx.coroutines.parMapUnordered
@OptIn(kotlinx.coroutines.FlowPreview::class, kotlinx.coroutines.ExperimentalCoroutinesApi::class)
//sampleStart
suspend fun main(): Unit {
flowOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
.parMapUnordered { a ->
delay(100)
a
}.toList() // [3, 5, 4, 6, 2, 8, 7, 1, 9, 10]
}
//sampleEnd
Content copied to clipboard