parSequence
suspend fun <A> Iterable<suspend () -> A>.parSequence(ctx: CoroutineContext = EmptyCoroutineContext): List<A>(source)
Sequences all tasks in parallel on Dispatchers.Default and return the result
Cancelling this operation cancels all running tasks.
import arrow.fx.coroutines.*
typealias Task = suspend () -> Unit
suspend fun main(): Unit {
//sampleStart
fun getTask(id: Int): Task =
suspend { println("Working on task $id on ${Thread.currentThread().name}") }
val res = listOf(1, 2, 3)
.map(::getTask)
.parSequence()
//sampleEnd
println(res)
}
Content copied to clipboard
suspend fun <A> Iterable<suspend CoroutineScope.() -> A>.parSequence(ctx: CoroutineContext = EmptyCoroutineContext): List<A>(source)
Sequences all tasks in parallel and return the result
Coroutine context is inherited from a CoroutineScope, additional context elements can be specified with ctx argument. If the combined context does not have any dispatcher nor any other ContinuationInterceptor, then Dispatchers.Default is used. WARNING If the combined context has a single threaded ContinuationInterceptor, this function will not run in parallel.
Cancelling this operation cancels all running tasks.
import arrow.fx.coroutines.*
import kotlinx.coroutines.Dispatchers
typealias Task = suspend () -> Unit
suspend fun main(): Unit {
//sampleStart
fun getTask(id: Int): Task =
suspend { println("Working on task $id on ${Thread.currentThread().name}") }
val res = listOf(1, 2, 3)
.map(::getTask)
.parSequence(Dispatchers.IO)
//sampleEnd
println(res)
}
Content copied to clipboard