Currying
In this page:
Currying
A curried function is declared with multiple parameter lists, like def add(a: Int)(b: Int): Int, letting you call it with all arguments at once or apply them one list at a time. Calling it with only the first parameter list returns a new function expecting the remaining ones. Currying is useful for building reusable, partially-configured functions and works especially well with Scala's syntax for passing a trailing function argument as a block.
Note: Curried functions are what enable Scala's block-syntax method calls, like list.foldLeft(0) { (acc, x) => acc + x }.
Example: Currying
object Main extends App {
def add(a: Int)(b: Int): Int = a + b
println(add(3)(4))
val addFive = add(5) _
println(addFive(10))
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: