BiFunction 接口中的 compose 方法是什么样的?

发布于 2025-01-14 22:40:11 字数 455 浏览 3 评论 0原文

Function 接口具有 compose()andThen() 方法,而 BiFunction 接口仅具有 >andThen() 方法。我的问题是如何实现相应的方法?我将尝试以图形方式表示这一点。

BiFunction 不带compose

单字母是 Java 的 FunctionBiFunction 接口定义的参数化类型。箭头表示输入和输出的流程。带有连接箭头的框是函数。虚线框只是展示了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.

BiFunction without compose

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天的雪花 2025-01-21 22:40:11

FunctionCompose()andThen() 方法非常简单,因为 Function 有一个输入和一个输出,因此只能以两种方式与另一个顺序串联。

由于 BiFunction 有一个输出,因此“after”函数必须是只有一个相应输入的函数,而 Function 符合要求。由于它有两个输入,“之前”函数需要有两个输出吗?你不能让一个方法返回两个东西,所以看起来不可能有“之前”。每个方法的返回类型与定义它们的接口相同,因此建议的方法应该返回一个 BiFunction

我的建议是一种将两个Function作为输入并返回一个BiFunction的方法。我不确定它还能是什么。它不能是两个 BiFunction,因为返回类型必须是 QuaterFunction

下面是在 Java 库中编写的代码:

public interface BiFunction<T, U, R> {
    // apply()...

    default <V, W> BiFunction<V, W, R> compose(
        Function<? super V, ? extends T> beforeLeft,
        Function<? super W, ? extends U> beforeRight) {
        
        Objects.requireNonNull(beforeLeft);
        Objects.requireNonNull(beforeRight);
        return (V v, W w) -> apply(beforeLeft.apply(v), beforeRight.apply(w));
    }

    // andThen()...
}

这是完成的图表:

在此处输入图像描述

这里正在使用它:

BiFunction<Integer, Integer, Integer> add = Integer::sum;
Function<Integer, Integer> abs = Math::abs;
BiFunction<Integer, Integer, Integer> addAbs = add.compose(abs, abs);
System.out.println(addAbs.apply(-2, -3));

// output: 5

如果你想实际测试它,你可以这样做:

public interface BiFunctionWithCompose<T, U, R> extends BiFunction<T, U, R> {...

或者像这样:

package myutil;
public interface BiFunction<T, U, R> extends java.util.function.BiFunction<T, U, R> {...

我不知道这是否对任何人都有用,但思考和写作真的很有趣。祝你有美好的一天。

The Function's Compose() and andThen() methods are straightforward since a Function 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, and Function 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 a BiFunction.

My proposal then is a method that takes two Functions as input and returns a BiFunction. I'm not sure what else it could even be. It couldn't be two BiFunctions because then the return type would have to be a QuaterFunction.

Here is the code as it would be written in the Java Library:

public interface BiFunction<T, U, R> {
    // apply()...

    default <V, W> BiFunction<V, W, R> compose(
        Function<? super V, ? extends T> beforeLeft,
        Function<? super W, ? extends U> beforeRight) {
        
        Objects.requireNonNull(beforeLeft);
        Objects.requireNonNull(beforeRight);
        return (V v, W w) -> apply(beforeLeft.apply(v), beforeRight.apply(w));
    }

    // andThen()...
}

Here is the finished graph:

enter image description here

Here it is in use:

BiFunction<Integer, Integer, Integer> add = Integer::sum;
Function<Integer, Integer> abs = Math::abs;
BiFunction<Integer, Integer, Integer> addAbs = add.compose(abs, abs);
System.out.println(addAbs.apply(-2, -3));

// output: 5

If you want to actually test this, you can do something like this:

public interface BiFunctionWithCompose<T, U, R> extends BiFunction<T, U, R> {...

Or like this:

package myutil;
public interface BiFunction<T, U, R> extends java.util.function.BiFunction<T, U, R> {...

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文