arrow-fx / arrow.fx / Semaphore / tryAcquireN

tryAcquireN

abstract fun tryAcquireN(n: Long): Kind<F, Boolean>

Try to acquires n resources and get an immediate response as Boolean.

import arrow.fx.*
import arrow.fx.extensions.io.async.async
import arrow.fx.extensions.io.monad.flatMap

fun main(args: Array<String>) {
  //sampleStart
  val semaphore = Semaphore.uncancellable<ForIO>(5, IO.async())

  semaphore.flatMap { s ->
    s.tryAcquireN(6)
  }.unsafeRunSync() == false

  semaphore.flatMap { s ->
    s.tryAcquireN(5)
  }.unsafeRunSync() == true
  //sampleEnd
}

Do you like Arrow?

Arrow Org
<