2017-12-02 8 views
0

Scalaでの関数型プログラミングでは、defという2つの形式の宣言がありました。しかし、私はそれらの間の相違点、およびこれに与えられた名前を知らない。私はこれについてより多くの情報を得ることができますか?高次関数

宣言1

def sum(f: Int => Int)(a: Int, b: Int): Int = ???

宣言2

def sum(f: Int => Int, a: Int, b: Int): Int = ???

答えて

2

まず一つはカリー構文と呼ばれています。

関数を部分的に適用して、新しい関数を返すことができます。

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/[email protected] 

第2のものは、複雑な構文ではありませんが、ここでもこの機能を部分的に適用できます。

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/[email protected] 

部分的に適用すると、やはり新しい関数が返されます。

上記の2つの宣言の違いはありません。構文的な砂糖だけです。