//arrow-fx-coroutines/arrow.fx.coroutines/parTraverse

parTraverse

common suspend fun <A, B> Iterable<A>.parTraverse(f: suspend .([A](par-traverse.html)) -> [B](par-traverse.html)): [List](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html)<[B](par-traverse.html)>

Traverses this Iterable and runs all mappers f on Dispatchers.Default. Cancelling this operation cancels all running tasks.

import arrow.fx.coroutines.*

data class User(val id: Int, val createdOn: String)

suspend fun main(): Unit {
  //sampleStart
  suspend fun getUserById(id: Int): User =
    User(id, Thread.currentThread().name)

  val res = listOf(1, 2, 3)
    .parTraverse { getUserById(it) }
 //sampleEnd
 println(res)
}

common suspend fun <A, B> Iterable<A>.parTraverse(ctx: CoroutineContext = EmptyCoroutineContext, f: suspend .([A](par-traverse.html)) -> [B](par-traverse.html)): [List](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html)<[B](par-traverse.html)>

Traverses this Iterable and runs all mappers f on CoroutineContext.

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

data class User(val id: Int, val createdOn: String)

suspend fun main(): Unit {
  //sampleStart
  suspend fun getUserById(id: Int): User =
    User(id, Thread.currentThread().name)

  val res = listOf(1, 2, 3)
    .parTraverse(Dispatchers.IO) { getUserById(it) }
 //sampleEnd
 println(res)
}

Do you like Arrow?

Arrow Org
<