arrow-fx / arrow.fx / Enqueue

Enqueue

interface ~~Enqueue~~<F, A> Deprecated: The IO datatype and it’s related type classes will disappear in Arrow 0.13.0. All useful operations are offered directly over suspend functions by Arrow Fx Coroutines. https://arrow-kt.io/docs/fx/async/

Enqueue allows offering values to a Queue, but doesn’t allow taking values from the Queue. You can use Enqueue to restrict certain functions or layers of your applications to only produce values.

import arrow.fx.*
import arrow.fx.extensions.*
import arrow.fx.typeclasses.*
import kotlin.coroutines.EmptyCoroutineContext

//sampleStart
suspend fun main(args: Array<String>): Unit = IO.fx {
  fun produceInts(e: Enqueue<ForIO, Int>, max: Int): IOOf<Unit> =
    (0..max).parTraverse(EmptyCoroutineContext) { i ->
      IO.sleep(i * 10.milliseconds).followedBy(e.offer(i))
    }.void()

  val queue = !Queue.unbounded<Int>()
  !produceInts(queue, 1000).fork()
  !IO.sleep(4.seconds)
  val res = !queue.takeAll()
  !effect { println(res) }
}.suspended()
//sampleEnd

See Also

Queue

Dequeue

Functions

offer Offers a value to the Queue, and behaves differently depending on the Queue.BackpressureStrategy.abstract fun offer(a: A): Kind<F, Unit>
offerAll abstract fun offerAll(a: Iterable<A>): Kind<F, Unit>
open fun offerAll(vararg a: A): Kind<F, Unit>
tryOffer Tries to offer a value to the Queue, it ignores the Queue.BackpressureStrategy and returns false if the Queue.BackpressureStrategy does not have room for the value.abstract fun tryOffer(a: A): Kind<F, Boolean>
tryOfferAll abstract fun tryOfferAll(a: Iterable<A>): Kind<F, Boolean>
open fun tryOfferAll(vararg a: A): Kind<F, Boolean>

Inheritors

Queue Lightweight Concurrent Queue for values of A.interface ~~Queue~~<F, A> : QueueOf<F, A>, Dequeue<F, A>, Enqueue<F, A>

Do you like Arrow?

Arrow Org
<