bind
Runs the EagerEffect to finish, returning B or shift in case of R.
import arrow.core.Either
import arrow.core.continuations.EagerEffect
import arrow.core.continuations.eagerEffect
import arrow.core.identity
import io.kotest.matchers.shouldBe
fun <E, A> Either<E, A>.toEagerEffect(): EagerEffect<E, A> = eagerEffect {
fold({ e -> shift(e) }, ::identity)
}
fun main() {
val either = Either.Left("failed")
eagerEffect<String, Int> {
val x: Int = either.toEagerEffect().bind()
x
}.toEither() shouldBe either
}
Content copied to clipboard
Folds Either into EagerEffect, by returning B or a shift with R.
import arrow.core.Either
import arrow.core.continuations.eagerEffect
import io.kotest.matchers.shouldBe
fun main() {
val either = Either.Right(9)
eagerEffect<String, Int> {
val x: Int = either.bind()
x
}.toEither() shouldBe either
}
Content copied to clipboard
Folds Validated into EagerEffect, by returning B or a shift with R.
import arrow.core.Validated
import arrow.core.continuations.eagerEffect
import io.kotest.matchers.shouldBe
fun main() {
val validated = Validated.Valid(40)
eagerEffect<String, Int> {
val x: Int = validated.bind()
x
}.toValidated() shouldBe validated
}
Content copied to clipboard
Folds Result into EagerEffect, by returning B or a transforming Throwable into R and shifting the result.
import arrow.core.continuations.eagerEffect
import arrow.core.identity
import io.kotest.matchers.shouldBe
private val default = "failed"
fun main() {
val result = Result.success(1)
eagerEffect<String, Int> {
val x: Int = result.bind { _: Throwable -> default }
x
}.fold({ default }, ::identity) shouldBe result.getOrElse { default }
}
Content copied to clipboard
Folds Option into EagerEffect, by returning B or a transforming None into R and shifting the result.
import arrow.core.None
import arrow.core.Option
import arrow.core.continuations.eagerEffect
import arrow.core.getOrElse
import arrow.core.identity
import io.kotest.matchers.shouldBe
private val default = "failed"
fun main() {
val option: Option<Int> = None
eagerEffect<String, Int> {
val x: Int = option.bind { default }
x
}.fold({ default }, ::identity) shouldBe option.getOrElse { default }
}
Content copied to clipboard