//arrow-fx-coroutines/arrow.fx.coroutines/parSequence

parSequence

common suspend fun <A> Iterable<suspend () -> A>.parSequence(): List<A>

suspend fun <A> Iterable<suspend () -> A>.parSequence(ctx: CoroutineContext = EmptyCoroutineContext): List<A>

common

@JvmName(name = “parSequenceScoped”)

suspend fun <A> Iterable<suspend .() -> [A](par-sequence.html)>.[parSequence](par-sequence.html)(): [List](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html)<[A](par-sequence.html)>

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)
}

common

@JvmName(name = “parSequenceScoped”)

suspend fun <A> Iterable<suspend .() -> [A](par-sequence.html)>.[parSequence](par-sequence.html)(ctx: [CoroutineContext](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.coroutines/-coroutine-context/index.html) = EmptyCoroutineContext): [List](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html)<[A](par-sequence.html)>

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)
}

Do you like Arrow?

Arrow Org
<