Skip to content

Instantly share code, notes, and snippets.

@DevSrSouza
Created October 5, 2021 15:39
Show Gist options
  • Save DevSrSouza/c717ab0719f9d76c040fa356943f7d31 to your computer and use it in GitHub Desktop.
Save DevSrSouza/c717ab0719f9d76c040fa356943f7d31 to your computer and use it in GitHub Desktop.
sealed class Either<out L, out R> {
class Left<L>(val value: L) : Either<L, Nothing>()
class Right<R>(val value: R) : Either<Nothing, R>()
fun fold(
onLeft: (L) -> Unit,
onRight: (R) -> Unit,
) {
when(this) {
is Left -> onLeft(value)
is Right -> onRight(value)
}
}
}
fun <R> right(value: R): Either<Nothing, R> = Either.Right(value)
fun <L> left(value: L): Either<L, Nothing> = Either.Left(value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment