Java “两者都消费”;“左”;及;对",;还是Vavr的?

Java “两者都消费”;“左”;及;对",;还是Vavr的?,java,vavr,Java,Vavr,我如何以功能性的方式同时使用a的“左”或“右” 我有一个返回或的方法。基于此结果,我需要执行对报告库的回调,例如reportSuccess()或reportFailure()。因此,我正在寻找一个很好的,实用的方法来做这件事。如果一个或有一个biConsumer(消费者有peek和peek left这两者结合在一起,与您所寻找的非常接近 void reportFailure(RuntimeException e) { System.out.println(e); } void repor

我如何以功能性的方式同时使用a的“左”或“右”


我有一个返回
的方法。基于此结果,我需要执行对报告库的回调,例如
reportSuccess()
reportFailure()
。因此,我正在寻找一个很好的,实用的方法来做这件事。如果一个
有一个
biConsumer(消费者有
peek
peek left
这两者结合在一起,与您所寻找的非常接近

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);
void报告失败(运行时异常e){
系统输出打印ln(e);
}
void reportSuccess(字符串值){
系统输出打印项次(值);
}
....
//印刷品:有些价值
或对=或对(“某个值”);
right.peek left(this::reportFailure).peek(this::reportSuccess);
//打印:java.lang.RuntimeException:某些错误
左任(
新的运行时异常(“某些错误”)
);
peek left(this::reportFailure).peek(this::reportSuccess);
Either<RuntimeException, String> mappedResult = result.bimap(ex -> {
  reportFailure();
  return ex;
}, str -> {
  reportSuccess();
  return str;
});
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);