Frage

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) _?

War es hilfreich?

Lösung

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

or

val timesThree = (x: Int) => multiply(x)(1)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top