Domanda

In Scala, I could generate a curried function like so:

def multiply(m: Int)(n: Int): Int = (m + 1) * (n + 2)

If I wanted, I could generate a new function, by filling that first parameter, like so:

val timesTwo = multiply(1) _

But what is the syntax for replacing the second argument, instead of the first?

val timesThree = multiply _ (1)  // Incorrect Syntax

More importantly, why is there not a direct parallel to multiply(1) _?

È stato utile?

Soluzione

val timesThree = multiply(_: Int)(1)

or

val timesThree = (x: Int) => multiply(x)(1)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top