I have a couple of Vavr Either's and I want to invoke a function with the Right
value for each of these Either's. For example:
Either<MyError, String> either1 = ..
Either<MyError, String> either2 = ..
Either<MyError, String> either3 = ..
Either<MyError, String>> methodRequiringAllInputs(String, String, String) {
..
}
I could of course do something like this:
either1.flatMap { value1 ->
either2.flatMap { value2 ->
either3.flatMap { value3 ->
methodRequiringAllInputs(value1, value2, value3);
}
}
}
But this is very ugly. In other languages you could just use something like do-notation or for comprehensions to flatten out the structure. I know that Vavr has the concept of a Validation which is an applicative functor that allows you to do:
Validation<MyError, String> validation1 = ..
Validation<MyError, String> validation2 = ..
Validation<MyError, String> validation3 = ..
Validation.combine(validation1, validation2, validation3)
.ap((validationValue1,validationValue2,validationValue3) -> .. );
which is much nicer.
My question is if something similar exists in Vavr for Either's to avoid the nested flatMap
structure? Note that I don't want to convert the Either
's to Validation
's.