arrow-fx / arrow.fx.typeclasses / Concurrent / raceN

raceN

open fun <A, B> CoroutineContext.raceN(fa: Kind<F, A>, fb: Kind<F, B>): Kind<F, Race2<A, B>>

Race two tasks concurrently within a new F on this@raceN. At the end of the race it automatically cancels the loser.

import arrow.Kind
import arrow.fx.*
import arrow.fx.typeclasses.Concurrent
import kotlinx.coroutines.Dispatchers
import arrow.fx.extensions.io.concurrent.concurrent

fun main(args: Array<String>) {
  fun <F> Concurrent<F>.example(): Kind<F, String> {
    val never: Kind<F, Int> = cancellable { effect { println("Never got cancelled for losing.") } }

    //sampleStart
    val result = fx.concurrent {
      val eitherGetOrUnit = !Dispatchers.Default.raceN(never, just(5))
      eitherGetOrUnit.fold(
        { "Never always loses race" },
        { i -> "Race was won with $i" }
      )
    }
  //sampleEnd
  return result
  }

  IO.concurrent().example().fix().unsafeRunSync().let(::println)
}

Parameters

this - @raceN CoroutineContext to execute the source F on.

fa - task to participate in the race

fb - task to participate in the race

Return F either Left if fa won the race, or Right if fb won the race.

See Also

racePair

open fun <A, B, C> CoroutineContext.raceN(fa: Kind<F, A>, fb: Kind<F, B>, fc: Kind<F, C>): Kind<F, Race3<A, B, C>> open fun <A, B, C, D> CoroutineContext.raceN(a: Kind<F, A>, b: Kind<F, B>, c: Kind<F, C>, d: Kind<F, D>): Kind<F, Race4<A, B, C, D>> open fun <A, B, C, D, E> CoroutineContext.raceN(a: Kind<F, A>, b: Kind<F, B>, c: Kind<F, C>, d: Kind<F, D>, e: Kind<F, E>): Kind<F, Race5<A, B, C, D, E>> open fun <A, B, C, D, E, G> CoroutineContext.raceN(a: Kind<F, A>, b: Kind<F, B>, c: Kind<F, C>, d: Kind<F, D>, e: Kind<F, E>, g: Kind<F, G>): Kind<F, Race6<A, B, C, D, E, G>> open fun <A, B, C, D, E, G, H> CoroutineContext.raceN(a: Kind<F, A>, b: Kind<F, B>, c: Kind<F, C>, d: Kind<F, D>, e: Kind<F, E>, g: Kind<F, G>, h: Kind<F, H>): Kind<F, Race7<A, B, C, D, E, G, H>> open fun <A, B, C, D, E, G, H, I> CoroutineContext.raceN(a: Kind<F, A>, b: Kind<F, B>, c: Kind<F, C>, d: Kind<F, D>, e: Kind<F, E>, g: Kind<F, G>, h: Kind<F, H>, i: Kind<F, I>): Kind<F, Race8<A, B, C, D, E, G, H, I>> open fun <A, B, C, D, E, G, H, I, J> CoroutineContext.raceN(a: Kind<F, A>, b: Kind<F, B>, c: Kind<F, C>, d: Kind<F, D>, e: Kind<F, E>, g: Kind<F, G>, h: Kind<F, H>, i: Kind<F, I>, j: Kind<F, J>): Kind<F, Race9<A, B, C, D, E, G, H, I, J>>

See Also

raceN

Do you like Arrow?

Arrow Org
<