Daniel Ryan
04/07/2021, 9:56 AMpublic suspend fun test(): Unit {
val add1: (number: Int) -> Int = { it + 1 }
suspend fun divideBy(number: Int, divideBy: Int): Either<Throwable, Int> {
return Either.catch { number / divideBy }
}
val result = either<Throwable, Int> {
val start = 1
val i1 = add1(start)
val i2 = !divideBy(i1, 2)
val i3 = add1(i2)
add1(i3)
}
result.fold(
{ println("failed: ${it.message}") },
{ println("result: $it") }
)
}
simon.vergauwen
04/07/2021, 10:01 AMdivideBy(add1(1), 2)
.map(add1)
.map(add1)
.fold(
{ println("failed: ${it.message}") },
{ println("result: $it") }
)
simon.vergauwen
04/07/2021, 10:01 AMmap
instead of andThen
to compose a function that work on the inner value of Either
.stojan
04/07/2021, 10:38 AMflatMap
for divideBy
Daniel Ryan
04/07/2021, 11:46 AMval add2 = add1 andThen add1
If feels like it should be possible to wrap the 'add1' function into a function that accepts an Either as an argument which would enable the functions to be composed together in a pipeline.
Sorry if this is a silly question. I have the information I needed, I am just trying to learn a bit more 🙂simon.vergauwen
04/07/2021, 3:13 PM(A) -> B
to a function (Either<E, A>) -> Either<E, B>
by using Either.lift
which is a function you'll also find in other places so.
val add1: (number: Int) -> Int = { it + 1 }
val add2: (number: Int) -> Int = add1 andThen add1
val eitherAdd: (Either<String, Int>) -> Either<String, Int> = Either.lift(add2)
Daniel Ryan
04/07/2021, 6:23 PM