javavavr

Consume both "left" and "right" of Vavr Either?


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.


Solution

  • There's peek and peekLeft that – in combination – are pretty close to what you are looking for.

    void reportFailure(RuntimeException e) {
        System.out.println(e);
    }
    void reportSuccess(String value) {
        System.out.println(value);
    }
    
    ....
    
    // prints: some value
    Either<RuntimeException, String> right = Either.right("some value");
    right.peekLeft(this::reportFailure).peek(this::reportSuccess);
    
    // prints: java.lang.RuntimeException: some error
    Either<RuntimeException, String> left = Either.left(
        new RuntimeException("some error")
    );
    left.peekLeft(this::reportFailure).peek(this::reportSuccess);