arrow-fx-coroutines / arrow.fx.coroutines / kotlin.collections.Iterable / parSequenceEither
suspend fun <A, B>
Iterable
<suspend () -> Either<A, B>>.parSequenceEither(): Either<A,
List
<B>>
Sequences all tasks in parallel on Dispatchers.Default and return the result. If one of the tasks returns Either.Left, then it will short-circuit the operation and cancelling all this running tasks, and returning the first encountered Either.Left.
Cancelling this operation cancels all running tasks.
suspend fun <A, B>
Iterable
<suspend () -> Either<A, B>>.parSequenceEither(ctx:
CoroutineContext
= EmptyCoroutineContext): Either<A,
List
<B>>
Sequences all tasks in parallel on ctx and return the result. If one of the tasks returns Either.Left, then it will short-circuit the operation and cancelling all this running tasks, and returning the first encountered Either.Left.
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.core.*
import arrow.fx.coroutines.*
import kotlinx.coroutines.Dispatchers
object Error
typealias Task = suspend () -> Either<Throwable, Unit>
suspend fun main(): Unit {
//sampleStart
fun getTask(id: Int): Task =
suspend { Either.catch { println("Working on task $id on ${Thread.currentThread().name}") } }
val res = listOf(1, 2, 3)
.map(::getTask)
.parSequenceEither(Dispatchers.IO)
//sampleEnd
println(res)
}
suspend fun <E, A>
Iterable
<suspend () -> Validated<E, A>>.parSequenceEither(semigroup: Semigroup<E>): Validated<E,
List
<A>>
Sequences all tasks in parallel on Dispatchers.Default and returns the result. If one or more of the tasks returns Validated.Invalid then all the Validated.Invalid results will be combined using semigroup.
Cancelling this operation cancels all running tasks.
Do you like Arrow?
✖