arrow-fx / arrow.fx / MVar / read

read

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

Reads the current value without emptying the MVar, assuming there is one, or otherwise it blocks until there is a value available.

import arrow.core.toT
import arrow.fx.*
import arrow.fx.extensions.io.async.async
import arrow.fx.extensions.io.monad.flatMap
import arrow.fx.extensions.io.monad.map
fun main(args: Array<String>) {
//sampleStart
val mvar = MVar.factoryUncancellable(IO.async())

mvar.just(5).flatMap { v ->
  v.read()
}.unsafeRunSync() == 5

mvar.just(5).flatMap { v ->
  v.read().flatMap { value ->
    v.isNotEmpty().map { isNotEmpty ->
      value toT isNotEmpty
    }
  }
}.unsafeRunSync() == 5 toT true

mvar.empty<Int>().flatMap { v ->
  v.read()
} //Never ends
//sampleEnd
}

Do you like Arrow?

Arrow Org
<