//arrow-fx-coroutines/arrow.fx.coroutines/parSequenceValidated

parSequenceValidated

common

@JvmName(name = “parSequenceValidatedScoped”)

suspend fun <E, A> Iterable<suspend CoroutineScope.() -> Validated<E, A».parSequenceValidated(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.

common suspend fun <E, A> Iterable<suspend () -> Validated<E, A».parSequenceValidated(semigroup: Semigroup<E>): Validated<E, List<A»

suspend fun <E, A> Iterable<suspend () -> Validated<E, A».parSequenceValidated(ctx: CoroutineContext = EmptyCoroutineContext, semigroup: Semigroup<E>): Validated<E, List<A»

common

@JvmName(name = “parSequenceValidatedScoped”)

suspend fun <E, A> Iterable<suspend CoroutineScope.() -> Validated<E, A».parSequenceValidated(ctx: CoroutineContext = EmptyCoroutineContext, semigroup: Semigroup<E>): Validated<E, List<A»

Sequences all tasks in parallel on ctx 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.

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.typeclasses.Semigroup
import arrow.fx.coroutines.*
import kotlinx.coroutines.Dispatchers

typealias Task = suspend () -> ValidatedNel<Throwable, Unit>

suspend fun main(): Unit {
  //sampleStart
  fun getTask(id: Int): Task =
    suspend { Validated.catchNel { println("Working on task $id on ${Thread.currentThread().name}") } }

  val res = listOf(1, 2, 3)
    .map(::getTask)
    .parSequenceValidated(Dispatchers.IO, Semigroup.nonEmptyList())
  //sampleEnd
  println(res)
}

Do you like Arrow?

Arrow Org
<