fold

inline fun <C> fold(ifLeft: (left: A) -> C, ifRight: (right: B) -> C): C

Transform an Either into a value of C. Alternative to using when to fold an Either into a value C.

import arrow.core.Either
import io.kotest.matchers.shouldBe
import io.kotest.assertions.fail

fun test() {
Either.Right(1)
.fold({ fail("Cannot be left") }, { it + 1 }) shouldBe 2

Either.Left(RuntimeException("Boom!"))
.fold({ -1 }, { fail("Cannot be right") }) shouldBe -1
}

Return

the transformed value C by applying ifLeft or ifRight to A or B respectively.

Parameters

ifLeft

transform the Either.Left type A to C.

ifRight

transform the Either.Right type B to C.