arrow-fx / arrow.fx / Dequeue / peek

peek

abstract fun peek(): Kind<F, A>

Peeks a value from the Queue or semantically blocks until a value becomes available. In contrast to take, peek does not remove the value from the Queue.

import arrow.fx.*
import arrow.fx.extensions.fx

//sampleStart
suspend fun main(args: Array<String>): Unit = IO.fx {
  val queue = !Queue.unbounded<Int>()
  val (join, _) = !queue.peek().fork()
  !queue.offer(1) // Removing this offer makes, !join block forever.
  val res = !join // Join the blocking peek, after we offered a value
  val res2 = !queue.peek() // We can peek again since it doesn't remove the value
  !effect { println("res: $res, res2: $res2") }
}.suspended()
//sampleEnd

See Also

take

tryPeek

Do you like Arrow?

Arrow Org
<