bind

open suspend fun <B> EagerEffect<R, B>.bind(): B

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
}

open suspend fun <B> Either<R, B>.bind(): B

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
}

open suspend fun <B> Validated<R, B>.bind(): B

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
}

open suspend fun <B> Result<B>.bind(transform: (Throwable) -> R): B

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 }
}

open suspend fun <B> Option<B>.bind(shift: () -> R): B

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 }
}