BiFunction接口中的默认andThen()方法 [英] Default andThen() method in BiFunction interface

查看:316
本文介绍了BiFunction接口中的默认andThen()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

BiFunction界面(java.util.function程序包)中有一个默认方法andThen().

default <V> BiFunction<T,U,V> andThen(Function<? super R,? extends V> after)

文档说:

返回一个组合函数,该函数首先将此函数应用于其输入,然后将after函数应用于该结果.如果对任何一个函数的求值抛出异常,则将其中继给组合函数的调用者.

了解解释的含义几乎没有什么困惑.据我了解,当调用默认的andThen()方法时,将返回一个组合函数.在返回类型为V的类型TU上调用此组合函数.最后,在类型RV上调用了after函数.

此方法有什么需要?它实际上如何适合图片?

解决方案

了解解释的含义是很容易混淆的.

为了尽可能简单地解释它,方法andThen返回一个函数,该函数首先将给定函数应用于输入,然后将另一个函数应用于该应用程序的结果.

假设我们有两个函数fg,函数f做一些逻辑,函数g做一些其他类型的逻辑,因此当您编写f.andThen(g)时,本质上是指g(f(x)),即我们首先应用作为参数f(x)给出的函数,然后将函数g应用于结果.

示例:

BiFunction<Integer, Integer, Integer> f = Math::addExact; 
Function<Integer, Integer> g = e -> e * 2; 
System.out.println(f.andThen(g).apply(10,10)); // 40

我们首先调用函数f(10, 10),然后取结果为20,将其传递给函数g(20),然后将其乘以20乘以2,从而得到40./p>

说实话,在Java中调用函数的语法并不是最好的,所以我可以理解,当有人第一次看这个时,可能很难理解,而且编写的内容越难理解例如,在C#中的函数可以简单地执行g(f(10, 10)),而在视觉上更容易理解,理解和理解.

此方法有什么需要?它实际上如何适应 图片?

以我的经验,像上面那样编写函数并不常见,但是我可以想象的一个典型场景是,如果您有各种实用程序方法执行某种逻辑,其中一个函数的结果进一步传递给其他函数,以便处理,在这种情况下,您可以通过组合实用程序方法使用函数组合来创建各种转换管道.

There's a default method andThen() in the BiFunction interface (java.util.function package).

default <V> BiFunction<T,U,V> andThen(Function<? super R,? extends V> after)

The documentation says:

Returns a composed function that first applies this function to its input, and then applies the after function to the result. If evaluation of either function throws an exception, it is relayed to the caller of the composed function.

It's little confusing to understand what the explanation means. As per my understanding, a composed function is returned when the default andThen() method is invoked. This composed function is invoked on the types T and U that returns the type V. Finally, there's and after function that is invoked on the types R and V.

What's the need of this method? How does it actually fit in the picture?

解决方案

It's little confusing to understand what the explanation means.

To explain it as simple as I can, the method andThen returns a function that first applies a given function to an input and then applies another function to the result of that application.

Assume we had two functions f and g , function f doing some logic and function g doing some other type of logic so when you compose f.andThen(g) that essentially means g(f(x)) i.e. we first apply the function given as argument f(x) and then apply the function g to the result.

Example:

BiFunction<Integer, Integer, Integer> f = Math::addExact; 
Function<Integer, Integer> g = e -> e * 2; 
System.out.println(f.andThen(g).apply(10,10)); // 40

We first call function f(10, 10) and then take the result of that which is 20, pass it to the function g(20) and that is executed multiplying 20 by 2 hence yielding 40.

To be honest the syntax to call a function in Java is not the best it can be so I can understand when someone looks at this the first time it might be difficult to grasp and gets harder to follow the more you compose functions, for example in C# one could simply do g(f(10, 10)) which visibly to the eye is easier to follow, read and understand.

What's the need of this method? How does it actually fit in the picture?

In my experience, it's not common that I've composed functions as shown above but a typical scenario I could imagine is if you have various utility methods that do some logic where the result of one function is further passed to other functions for processing in which case you can then use function composition to create various transformation pipelines by composing the utility methods.

这篇关于BiFunction接口中的默认andThen()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆