commonMain.extensions.EitherMap.kt Maven / Gradle / Ivy
package com.javiersc.either.extensions
import com.javiersc.either.Either
/** Map an Either to another completely different Either */
public fun Either.map(
left: (L) -> NL,
right: (R) -> NR,
): Either =
when (this) {
is Either.Left -> Either.Left(left(this.left))
is Either.Right -> Either.Right(right(this.right))
}
/** Map left part of an Either with the same Right type */
public fun Either.mapLeft(
left: (L) -> NL,
): Either =
when (this) {
is Either.Left -> Either.Left(left(this.left))
is Either.Right -> this
}
/** Map right part of an Either with the same Left type */
public fun Either.mapRight(
right: (R) -> NR,
): Either =
when (this) {
is Either.Left -> this
is Either.Right -> Either.Right(right(this.right))
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy