How can I consume both a "left" or a "right" of a vavr Either in a functional way?
I have a method that returns an Either<RuntimeException, String>
. Based on this result, I need to execute a callback to our reporting library, e.g. reportSuccess()
or reportFailure()
. Consequently, I am looking for a nice, functional way of doing it. If an Either
had a biConsumer(Consumer<? super L> leftConsumer, Consumer<? super R> rightConsumer
, I could write something like:
Either<RuntimeException, String> result = // get the result from somewhere
result.biConsumer(ex -> {
reportFailure();
}, str -> {
repportSuccess();
});
The closest workaround I have found so far is the biMap() method, which would look something like
Either<RuntimeException, String> mappedResult = result.bimap(ex -> {
reportFailure();
return ex;
}, str -> {
reportSuccess();
return str;
});
Arguably, mapping functions should be used for mapping and not side effects, so even if it works I am looking for alternatives.
There's
peek
andpeekLeft
that – in combination – are pretty close to what you are looking for.