← Back to Scala Course | Chapter 9: Functional Programming | Lesson 5 of 8

Currying

In this page:

  1. Currying

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

markup
object Main extends App {
  def add(a: Int)(b: Int): Int = a + b

  println(add(3)(4))

  val addFive = add(5) _
  println(addFive(10))
}
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.