All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.fr.acinq.lightning.utils.Either.kt Maven / Gradle / Ivy

There is a newer version: 1.8.4
Show newest version
package fr.acinq.lightning.utils

sealed class Either {
    abstract val isLeft: Boolean
    abstract val isRight: Boolean
    abstract val left: A?
    abstract val right: B?

    fun  fold(fa: (A) -> X, fb: (B) -> X): X = when (this) {
        is Left -> fa(this.value)
        is Right -> fb(this.value)
    }

    fun  transform(fa: (A) -> X, fb: (B) -> Y): Either = when (this) {
        is Left -> Left(fa(this.value))
        is Right -> Right(fb(this.value))
    }

    data class Left(val value: A) : Either() {
        override val isLeft = true
        override val isRight = false
        override val left: A? = value
        override val right = null
    }

    data class Right(val value: B) : Either() {
        override val isLeft = false
        override val isRight = true
        override val left = null
        override val right = value
    }
}

@Suppress("UNCHECKED_CAST")
fun  Either.flatMap(f: (R) -> Either): Either =
    this.fold({ this as Either }, f)

fun  Either.map(f: (R) -> T): Either =
    flatMap { Either.Right(f(it)) }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy