bind
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
}
Content copied to clipboard
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
}
Content copied to clipboard
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
}
Content copied to clipboard
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 }
}
Content copied to clipboard
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 }
}
Content copied to clipboard