parTraverseValidated
Traverses this Iterable and runs all mappers f on Dispatchers.Default. If one or more of the f returns Validated.Invalid then all the Validated.Invalid results will be combined using semigroup.
Cancelling this operation cancels all running tasks.
Traverses this Iterable and runs all mappers f on CoroutineContext. If one or more of the f 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
object Error
data class User(val id: Int, val createdOn: String)
suspend fun main(): Unit {
//sampleStart
suspend fun getUserById(id: Int): ValidatedNel<Error, User> =
if(id % 2 == 0) Error.invalidNel()
else User(id, Thread.currentThread().name).validNel()
val res = listOf(1, 3, 5)
.parTraverseValidated(Dispatchers.IO, Semigroup.nonEmptyList()) { getUserById(it) }
val res2 = listOf(1, 2, 3, 4, 5)
.parTraverseValidated(Dispatchers.IO, Semigroup.nonEmptyList()) { getUserById(it) }
//sampleEnd
println(res)
println(res2)
}