fold

abstract suspend fun <B> fold(recover: suspend (R) -> B, transform: suspend (A) -> B): B

Runs the suspending computation by creating a Continuation, and running the fold function over the computation.

When the Cont has shifted with R it will recover the shifted value to B, and when it ran the computation to completion it will transform the value A to B.

suspend fun test() {
val shift = cont<String, Int> {
shift("Hello, World!")
}.fold({ str: String -> str }, { int -> int.toString() })
shift shouldBe "Hello, World!"

val res = cont<String, Int> {
1000
}.fold({ str: String -> str.length }, { int -> int })
res shouldBe 1000
}

open suspend fun <B> fold(error: suspend (error: Throwable) -> B, recover: suspend (R) -> B, transform: suspend (A) -> B): B

Like fold but also allows folding over any unexpected Throwable that might have occurred.

See also