在Scala的funcional编程过程中,我看到了def的两种声明形式。但我不知道他们之间的区别,以及他们的名字。我怎么能得到更多这方面的信息?
声明1
def sum(f: Int => Int)(a: Int, b: Int): Int = ???
声明2
def sum(f: Int => Int, a: Int, b: Int): Int = ???
发布于 2017-12-02 17:33:16
第一种被称为咖喱语法。
您可以部分应用该函数,然后返回一个新函数。
scala> def sum(f: Int => Int)(a: Int, b: Int): Int = f(a) + f(b)
sum: (f: Int => Int)(a: Int, b: Int)Int
scala> sum({x: Int => x + 1}) _
res10: (Int, Int) => Int = $$Lambda$1115/108209958@474821de第二种是非仓促语法,但我们仍然可以部分地应用该函数,即使在这种情况下也是如此。
scala> def sum(f: Int => Int, a: Int, b: Int): Int = f(a) + f(b)
sum: (f: Int => Int, a: Int, b: Int)Int
scala> sum({x: Int => x + 1}, _: Int, _: Int)
res11: (Int, Int) => Int = $$Lambda$1116/1038002783@1a500561同样,新函数在部分应用时返回。
这两种说法没有什么区别,只是句法上的一种糖。
https://stackoverflow.com/questions/47610508
复制相似问题