相关疑难解决方法(0)

在Java 8中,带有2个箭头的lambda是什么意思?

我以前读了几篇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 lambda currying java-8

118
推荐指数
6
解决办法
1万
查看次数

怎么做功能组合?

虽然不耐烦地等待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中实现这一点?

java java-8

33
推荐指数
2
解决办法
1万
查看次数

标签 统计

java ×2

java-8 ×2

currying ×1

lambda ×1