fold

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

Runs the non-suspending computation by creating a Continuation with an EmptyCoroutineContext, and running the fold function over the computation.

When the EagerEffect 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.eagerEffect
import io.kotest.matchers.shouldBe

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

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

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

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

See also