when working with an `Either` I sometimes run into...
# functional
r
when working with an
Either
I sometimes run into having multiple results where I need the results for another call. There I'm usually end up in an ugly structure with nested `flatMap`s. E.g.:
Copy code
val result1 = foo() // : Either<Error, Result1>
val result2 = foo() // : Either<Error, Result2>
val result3 = foo() // : Either<Error, Result3>

val transformed = result1.flatMap { res1 ->
    result2.flatMap { res2 ->
        result3.flatMap { res3 -> someCall(res1, res2, res3) }
    }
}
isn't there a nicer way to do this?
one shot (that I'm not satisfied with) is to use a
flatMap
curry structure:
Copy code
Right(someCall)
    .flatMap { fn ->
        foo().map { res1 -> {res2: Result2, res3: Result3 -> fn(res1, res2, res3) } }
    }
    .flatMap { fn ->
        foo().map { res2 -> {res3: Result3 -> fn(res2, res3) } }
    }
    .flatMap { fn ->
        foo().map { fn(it) } }
    }
y
Use the
either
continuation block
Copy code
either<Error> {
  someCall(result1.bind(), result2.bind(), result3.bind()
}
Or alternatively use
Either.zip
Copy code
result1.zip(result2, result3, ::someCall)
r
thanks, I will look into that 🙂