Currying in Scala: multiple parameter lists and function return

Using the following syntax to define functions with currying enabled:

def sum(x: Int)(y: Int)(z: Int) = x + y + z

it is still necessary to suffix any calls to drawn calls sumwith _:

sum _
sum(3) _
sum(3)(2) _

otherwise the compiler will complain.

Therefore, I resorted to:

val sum = (x: Int) => (y: Int) => (z: Int) => x + y + z

which works without _.

Now the question is: why is it necessary for a version with multiple list parameters _to perform currying? Why is the semantics of these two versions not equivalent in all contexts?

Also, is the latest version somehow discouraged? Does he have any reservations?

+4
1

, , , - .

JVM, (, , Function1, Function2 ..).

,

def sum(x: Int)(y: Int)(z: Int) = x + y + z

val sum = (x: Int) => (y: Int) => (z: Int) => x + y + z

, , - Function1[Int, Function1[Int, Function1[Int, Int]]]

, , (, eta-expand).

, , , , , , .

_ eta-, , .

scala , :

def sum(x: Int)(y: Int)(z: Int) = x + y + z
val sumFunction: Int => Int => Int => Int = sum

,

def sum(x: Int, y: Int) = x + y
List(1,2,3).reduce(sum)

, .. , .

, scala eta-: fooobar.com/questions/167436/...


, , , .

+4

All Articles