gpt4 book ai didi

java - Java8 中是否有标准化的通用函数式接口(interface)?

转载 作者:行者123 更新时间:2023-11-30 08:35:02 25 4
gpt4 key购买 nike

我想知道是否以及在哪里有人定义了一组通用函数接口(interface)来简化和标准化 lambda 的使用。我来自 C#,核心库完成了这项工作。

我读到“BiFunction”,发现它很笨拙。特别是当扩展到 TriFunction 等时。

我想到了以下内容:

public interface FI {
@FunctionalInterface
public interface Action<T> {
void invoke(T arg1);
}
@FunctionalInterface
public interface Function0<TResult> {
TResult invoke();
}
@FunctionalInterface
public interface Function1<Targ1, TResult> {
TResult invoke(Targ1 arg1);
}
@FunctionalInterface
public interface Function2<Targ1, Targ2, TResult> {
TResult invoke(Targ1 arg1, Targ2 arg2);
}
@FunctionalInterface
public interface Function3<Targ1, Targ2, Targ3, TResult> {
TResult invoke(Targ1 arg1, Targ2 arg2, Targ3 arg3);
}
@FunctionalInterface
public interface Function4<Targ1, Targ2, Targ3, Targ4, TResult> {
TResult invoke(Targ1 arg1, Targ2 arg2, Targ3 arg3, Targ4 arg4);
}
@FunctionalInterface
public interface Function5<Targ1, Targ2, Targ3, Targ4, Targ5, TResult> {
TResult invoke(Targ1 arg1, Targ2 arg2, Targ3 arg3, Targ4 arg4, Targ5 arg5);
}
@FunctionalInterface
public interface Function6<Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, TResult> {
TResult invoke(Targ1 arg1, Targ2 arg2, Targ3 arg3, Targ4 arg4, Targ5 arg5, Targ6 arg6);
}
@FunctionalInterface
public interface Function7<Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, TResult> {
TResult invoke(Targ1 arg1, Targ2 arg2, Targ3 arg3, Targ4 arg4, Targ5 arg5, Targ6 arg6, Targ7 arg7);
}
@FunctionalInterface
public interface Function8<Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, TResult> {
TResult invoke(Targ1 arg1, Targ2 arg2, Targ3 arg3, Targ4 arg4, Targ5 arg5, Targ6 arg6, Targ7 arg7, Targ8 atg8);
}
@FunctionalInterface
public interface Function9<Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, TResult> {
TResult invoke(Targ1 arg1, Targ2 arg2, Targ3 arg3, Targ4 arg4, Targ5 arg5, Targ6 arg6, Targ7 arg7, Targ8 atg8, Targ9 arg9);
}
@FunctionalInterface
public interface Function10<Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10, TResult> {
TResult invoke(Targ1 arg1, Targ2 arg2, Targ3 arg3, Targ4 arg4, Targ5 arg5, Targ6 arg6, Targ7 arg7, Targ8 atg8, Targ9 arg9, Targ10 arg10);
}

}

最佳答案

javaslang[1] 提供了Function0-Function8甚至CheckedFunction0-CheckedFunction8。它们甚至提供额外的功能,如“记忆化”和“柯里化(Currying)”。

http://javaslang.io

关于java - Java8 中是否有标准化的通用函数式接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38475683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com