怎么做功能组合? [英] How to do function composition?
本文介绍了怎么做功能组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
虽然不耐烦地等待Java 8发布并且在阅读了精彩的'状态来自Brian Goetz的Lambda的文章我注意到功能组合根本没有涉及。
While rather impatiently waiting for Java 8 release and after reading brilliant 'State of the Lambda' article from Brian Goetz I noticed that function composition was not covered at all.
根据上述文章,在Java 8中应该可以使用以下内容:
As per above article, in Java 8 the following should be possible:
// 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;
现在,如果我想将这两个函数组合成函数映射人
到国家,如何在Java 8中实现这一点?
Now if I would like to compose these two functions to have a function mapping Person
to country, how can I achieve this in Java 8?
推荐答案
有一个默认界面function Function :: andThen
和 Function :: compose
:
There is a default interface function Function::andThen
and Function::compose
:
Function<Person, String> toCountry = personToAddress.andThen(addressToCountry);
这篇关于怎么做功能组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文