Som*_*kar 3 java lambda functional-programming java-8
在Java中的函数编程一书中,作者仅使用Function<T, U>接口构建了一个compose函数(然而,该接口不是Java 8附带的,但非常相似),其片段如下所示:
public interface Function<T, U> {
U apply(T arg);
}
Run Code Online (Sandbox Code Playgroud)
虽然我可以理解下面的compose的方法版本,它接受2个函数并返回一个组合函数
public static final Function<Integer, Integer> compose (final Function<Integer, Integer> f1,
final Function<Integer, Integer> f2) {
arg -> f1.apply(f2.apply(arg));
}
Run Code Online (Sandbox Code Playgroud)
我只是无法通过Function <>和lambdas来了解下面的compose实现
static Function<Function<Integer, Integer>,
Function<Function<Integer, Integer>,
Function<Integer, Integer>>> compose =
x -> y -> z -> x.apply(y.apply(z));
Run Code Online (Sandbox Code Playgroud)
PS:无法理解我的想法并继续前进其余部分:(
将a Function<Integer, Integer>作为一个整数函数,你所拥有的是一个函数,它将{一个整数函数}映射到一个函数,将整数函数映射到另一个整数函数.
具体来说,当给定一个整数函数时x,它返回一个函数,给定一个整数函数y,返回一个将整数映射z到的整数函数x(y(z)).
Given a function x
Returns a function that:
Given a function y
Returns a function that:
Given an integer z
Returns x(y(z))
Run Code Online (Sandbox Code Playgroud)