arrow-fx / arrow.fx / Queue / bounded

bounded

fun <F, A> bounded(capacity: Int, CF: Concurrent<F>): Kind<F, Queue<F, A>>

Create a Queue with BackpressureStrategy.Bounded.

Offering to a bounded queue at capacity will cause the fiber making the call to be suspended until the queue has space to receive the offered value.

import arrow.fx.*
import arrow.fx.extensions.fx
import arrow.fx.extensions.io.concurrent.concurrent

suspend fun main(args: Array<String>): Unit = IO.fx {
  val capacity = 2
  val q = !Queue.bounded<Int>(capacity)
  !q.offer(42)
  !q.offer(43)
  !q.offer(44).fork() // <-- This `offer` exceeds the capacity and will be suspended
  val fortyTwo   = !q.take()
  val fortyThree = !q.take()
  val fortyFour  = !q.take()
  !effect { println(listOf(fortyTwo, fortyThree, fortyFour)) }
}.suspended()

Do you like Arrow?

Arrow Org
<