-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEither.kt
32 lines (26 loc) · 982 Bytes
/
Either.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
sealed class Either<out L, out R> {
data class Left<T>(val value: T) : Either<T, Nothing>()
data class Right<T>(val value: T) : Either<Nothing, T>()
}
inline fun <T> either(f: () -> T): Either<Exception, T> =
try {
Either.Right(f())
} catch (e: Exception) {
Either.Left(e)
}
inline infix fun <A, B, C> Either<A, B>.map(f: (B) -> C): Either<A, C> = when(this) {
is Either.Left -> this
is Either.Right -> Either.Right(f(this.value))
}
inline infix fun <A, B, C> Either<A, B>.flatMap(f: (B) -> Either<A, C>): Either<A, C> = when(this) {
is Either.Left -> this
is Either.Right -> f(value)
}
inline infix fun <A, B, C> Either<A, C>.mapError(f: (A) -> B): Either<B, C> = when(this) {
is Either.Left -> Either.Left(f(value))
is Either.Right -> this
}
inline fun <A, B, C> Either<A, B>.fold(left: (A) -> C, right: (B) -> C): C = when(this) {
is Either.Left -> left(this.value)
is Either.Right -> right(this.value)
}