BiFunction 接口中的 compose 方法是什么样的?
Function
接口具有 compose()
和 andThen()
方法,而 BiFunction
接口仅具有 >andThen()
方法。我的问题是如何实现相应的方法?我将尝试以图形方式表示这一点。
单字母是 Java 的 Function
和 BiFunction
接口定义的参数化类型。箭头表示输入和输出的流程。带有连接箭头的框是函数。虚线框只是展示了apply方法是如何使用的。
The Function
interface has the compose()
and andThen()
methods while the BiFunction
interface only has the andThen()
method. My question is simply how could the corresponding method be implemented? I'll try to represent this graphically.
The single letters are parameterized types as defined by Java's Function
and BiFunction
interfaces. Arrows are the flow of inputs and outputs. Boxes with connected arrows are functions. The dotted box just shows how the apply method is used.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Function
的Compose()
和andThen()
方法非常简单,因为Function
有一个输入和一个输出,因此只能以两种方式与另一个顺序串联。由于
BiFunction
有一个输出,因此“after”函数必须是只有一个相应输入的函数,而Function
符合要求。由于它有两个输入,“之前”函数需要有两个输出吗?你不能让一个方法返回两个东西,所以看起来不可能有“之前”。每个方法的返回类型与定义它们的接口相同,因此建议的方法应该返回一个BiFunction
。我的建议是一种将两个
Function
作为输入并返回一个BiFunction
的方法。我不确定它还能是什么。它不能是两个BiFunction
,因为返回类型必须是QuaterFunction
。下面是在 Java 库中编写的代码:
这是完成的图表:
这里正在使用它:
如果你想实际测试它,你可以这样做:
或者像这样:
我不知道这是否对任何人都有用,但思考和写作真的很有趣。祝你有美好的一天。
The
Function
'sCompose()
andandThen()
methods are straightforward since aFunction
has one input and one output and therefore can only be strung sequentially with another in two ways.Since a
BiFunction
has one output, the "after" function has to be something with only one corresponding input, andFunction
fits the bill. And since it has two inputs, the "before" function needs to be something with two outputs? You can't have a method return two things, so there seemingly can't be a "before". The return type of each of these methods is the same as the interface they are defined in, so the proposed method should return aBiFunction
.My proposal then is a method that takes two
Function
s as input and returns aBiFunction
. I'm not sure what else it could even be. It couldn't be twoBiFunction
s because then the return type would have to be aQuaterFunction
.Here is the code as it would be written in the Java Library:
Here is the finished graph:
Here it is in use:
If you want to actually test this, you can do something like this:
Or like this:
I have no idea if this will be useful to anyone, but it was really fun to think through and write. Have a wonderful day.