commonMain.fr.acinq.lightning.utils.Either.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lightning-kmp Show documentation
Show all versions of lightning-kmp Show documentation
A Kotlin Multiplatform implementation of the Lightning Network
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