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

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

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)方法,我可以写出类似以下的代码:

Either<RuntimeException, String> result = // 从某处获取结果

result.biConsumer(ex -> {
  reportFailure();
}, str -> {
  reportSuccess();
});

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

Either<RuntimeException, String> mappedResult = result.bimap(ex -> {
  reportFailure();
  return ex;
}, str -> {
  reportSuccess();
  return str;
});

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

英文:

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:

Either&lt;RuntimeException, String&gt; result = // get the result from somewhere

result.biConsumer(ex -&gt; {
  reportFailure();
}, str -&gt; {
  repportSuccess();
});

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

Either&lt;RuntimeException, String&gt; mappedResult = result.bimap(ex -&gt; {
  reportFailure();
  return ex;
}, str -&gt; {
  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.

答案1

得分: 3

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

void reportFailure(RuntimeException e) {
    System.out.println(e);
}
void reportSuccess(String value) {
    System.out.println(value);
}

....

// 输出:some value
Either<RuntimeException, String> right = Either.right("some value");
right.peekLeft(this::reportFailure).peek(this::reportSuccess);

// 输出:java.lang.RuntimeException: some error
Either<RuntimeException, String> left = Either.left(
    new RuntimeException("some error")
);
left.peekLeft(this::reportFailure).peek(this::reportSuccess);
英文:

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&lt;RuntimeException, String&gt; right = Either.right(&quot;some value&quot;);
right.peekLeft(this::reportFailure).peek(this::reportSuccess);

// prints: java.lang.RuntimeException: some error
Either&lt;RuntimeException, String&gt; left = Either.left(
    new RuntimeException(&quot;some error&quot;)
);
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:

确定