我以前读了几篇Java 8教程.
现在我遇到了以下主题: java支持Currying吗?
在这里,我看到以下代码:
IntFunction<IntUnaryOperator> curriedAdd = a -> b -> a + b;
System.out.println(curriedAdd.apply(1).applyAsInt(12));
Run Code Online (Sandbox Code Playgroud)
我明白这个例子总结了2个元素,但我无法理解构造:
a -> b -> a + b;
Run Code Online (Sandbox Code Playgroud)
根据表达式的左侧部分,该行应实现以下功能:
R apply(int value);
Run Code Online (Sandbox Code Playgroud)
在此之前,我只用一支箭只遇到了lambdas.
虽然不耐烦地等待Java 8发布,在阅读Brian Goetz的精彩"Lambda状态"文章之后,我注意到功能组合根本没有被覆盖.
根据上面的文章,在Java 8中应该可以:
// having classes Address and Person
public class Address {
private String country;
public String getCountry() {
return country;
}
}
public class Person {
private Address address;
public Address getAddress() {
return address;
}
}
// we should be able to reference their methods like
Function<Person, Address> personToAddress = Person::getAddress;
Function<Address, String> addressToCountry = Address::getCountry;
Run Code Online (Sandbox Code Playgroud)
现在,如果我想将这两个函数组合成一个函数映射Person到country,我怎样才能在Java 8中实现这一点?