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 Effect 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.

import arrow.core.continuations.effect
import io.kotest.matchers.shouldBe

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

val res = effect<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