bind

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

Runs the Cont to finish, returning B or shift in case of R.

fun <E, A> Either<E, A>.toCont(): Cont<E, A> = cont {
fold({ e -> shift(e) }, ::identity)
}

suspend fun test() = checkAll(Arb.either(Arb.string(), Arb.int())) { either ->
cont<String, Int> {
val x: Int = either.toCont().bind()
x
}.toEither() shouldBe either
}

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

Folds Either into Cont, by returning B or a shift with R.

suspend fun test() = checkAll(Arb.either(Arb.string(), Arb.int())) { either ->
cont<String, Int> {
val x: Int = either.bind()
x
}.toEither() shouldBe either
}

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

Folds Validated into Cont, by returning B or a shift with R.

suspend fun test() = checkAll(Arb.validated(Arb.string(), Arb.int())) { validated ->
cont<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 Cont, by returning B or a transforming Throwable into R and shifting the result.

private val default = "failed"
suspend fun test() = checkAll(Arb.result(Arb.int())) { result ->
cont<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 Cont, by returning B or a transforming None into R and shifting the result.

private val default = "failed"
suspend fun test() = checkAll(Arb.option(Arb.int())) { option ->
cont<String, Int> {
val x: Int = option.bind { default }
x
}.fold({ default }, ::identity) shouldBe option.getOrElse { default }
}