arrow-fx / arrow.fx.typeclasses / Concurrent / parSequence

parSequence

open fun <G, A> Kind<G, Kind<F, A>>.parSequence(TG: Traverse<G>, ctx: CoroutineContext): Kind<F, Kind<G, A>>

Runs all the F effects of the G structure to invert the structure from Kind<F, Kind<G, A>> to Kind<G, Kind<F, A>>.

import arrow.Kind
import arrow.fx.IO
import arrow.fx.extensions.io.concurrent.concurrent
import arrow.fx.fix
import arrow.fx.typeclasses.Async
import arrow.fx.typeclasses.Concurrent

data class User(val id: Int)

fun main() {
  fun <F> Async<F>.getUserById(id: Int): Kind<F, User> =
    effect { User(id) }

  fun <F> Concurrent<F>.processInParallel(): Kind<F, List<User>> =
  //sampleStart
    listOf(1, 2, 3)
      .map { id -> getUserById(id) }
      .parSequence()
 //sampleEnd
  IO.concurrent().processInParallel()
    .fix().unsafeRunSync()
}

Note: This uses parTraverse so the same caveats apply. Do not use this on huge collections or infinite sequences.

See Also

parTraverse

open fun <G, A> Kind<G, Kind<F, A>>.parSequence(TG: Traverse<G>): Kind<F, Kind<G, A>> open fun <A> Iterable<Kind<F, A>>.parSequence(ctx: CoroutineContext): Kind<F, List<A>> open fun <A> Iterable<Kind<F, A>>.parSequence(): Kind<F, List<A>>

See Also

parSequence

Do you like Arrow?

Arrow Org
<