arrow-fx / arrow.fx / MVar / take
abstract fun take(): Kind<F, A>
Empties the MVar if full, returning the value, or blocks otherwise until a value is available.
import arrow.fx.*
import arrow.fx.extensions.io.async.async
import arrow.fx.extensions.io.monad.flatMap
fun main(args: Array<String>) {
//sampleStart
val mvar = MVar.factoryUncancellable(IO.async())
mvar.just(5).flatMap { v ->
v.take()
}.unsafeRunSync() == 5
mvar.empty<Int>().flatMap { v ->
v.take()
} //Never ends
//sampleEnd
}
Do you like Arrow?
✖