消耗Vavr Either的“left”和“right”两个部分?

huangapple go评论100阅读模式
英文:

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

问题

如何以函数式的方式同时消费vavr中的“left”或“right”Either

我有一个返回Either<RuntimeException, String>的方法。基于这个结果,我需要执行一个回调到我们的报告库,例如reportSuccess()reportFailure()。因此,我正在寻找一种优雅的函数式方法来实现这一点。如果Either有一个biConsumer(Consumer<? super L> leftConsumer, Consumer<? super R> rightConsumer)方法,我可以写出类似以下的代码:

  1. Either<RuntimeException, String> result = // 从某处获取结果
  2. result.biConsumer(ex -> {
  3. reportFailure();
  4. }, str -> {
  5. reportSuccess();
  6. });

到目前为止,我找到的最接近的解决方法是使用biMap()方法,代码类似于:

  1. Either<RuntimeException, String> mappedResult = result.bimap(ex -> {
  2. reportFailure();
  3. return ex;
  4. }, str -> {
  5. reportSuccess();
  6. return str;
  7. });

可以说,映射函数应该用于映射而不是副作用,所以即使它能工作,我还在寻找其他的解决方法。

英文:

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&lt;RuntimeException, String&gt;. 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&lt;? super L&gt; leftConsumer, Consumer&lt;? super R&gt; rightConsumer, I could write something like:

  1. Either&lt;RuntimeException, String&gt; result = // get the result from somewhere
  2. result.biConsumer(ex -&gt; {
  3. reportFailure();
  4. }, str -&gt; {
  5. repportSuccess();
  6. });

The closest workaround I have found so far is the biMap() method, which would look something like

  1. Either&lt;RuntimeException, String&gt; mappedResult = result.bimap(ex -&gt; {
  2. reportFailure();
  3. return ex;
  4. }, str -&gt; {
  5. reportSuccess();
  6. return str;
  7. });

Arguably, mapping functions should be used for mapping and not side effects, so even if it works I am looking for alternatives.

答案1

得分: 3

peekpeekLeft,结合使用,非常接近您所寻找的内容。

  1. void reportFailure(RuntimeException e) {
  2. System.out.println(e);
  3. }
  4. void reportSuccess(String value) {
  5. System.out.println(value);
  6. }
  7. ....
  8. // 输出:some value
  9. Either<RuntimeException, String> right = Either.right("some value");
  10. right.peekLeft(this::reportFailure).peek(this::reportSuccess);
  11. // 输出:java.lang.RuntimeException: some error
  12. Either<RuntimeException, String> left = Either.left(
  13. new RuntimeException("some error")
  14. );
  15. left.peekLeft(this::reportFailure).peek(this::reportSuccess);
英文:

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

  1. void reportFailure(RuntimeException e) {
  2. System.out.println(e);
  3. }
  4. void reportSuccess(String value) {
  5. System.out.println(value);
  6. }
  7. ....
  8. // prints: some value
  9. Either&lt;RuntimeException, String&gt; right = Either.right(&quot;some value&quot;);
  10. right.peekLeft(this::reportFailure).peek(this::reportSuccess);
  11. // prints: java.lang.RuntimeException: some error
  12. Either&lt;RuntimeException, String&gt; left = Either.left(
  13. new RuntimeException(&quot;some error&quot;)
  14. );
  15. left.peekLeft(this::reportFailure).peek(this::reportSuccess);

huangapple
  • 本文由 发表于 2020年9月12日 20:10:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63860172.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定