Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 37 ยท Functional Programming

Higher-Order Functions and Currying in Scala

Learn Scala functions as values: lambdas, function types, map, filter and fold, closures, composition with andThen, partial application and currying.

In Scala a function is a value like any other. You can store it in a val, pass it to another function, and return it from one. A function that takes or returns functions is called a higher-order function. They are the main reason Scala collections code is so compact: instead of writing a loop, you describe what to do with each element and hand that description to map, filter or fold.

Function values and types

A function literal (lambda) is written with =>:

SCALA
val double: Int => Int = x => x * 2
val add: (Int, Int) => Int = (a, b) => a + b
val greet: String => String = name => s"Hello, $name"

println(double(21))      // 42
println(add(2, 3))       // 5
println(greet("Ada"))    // Hello, Ada

The type Int => Int means "a function from Int to Int". (Int, Int) => Int takes two arguments. A function with no arguments has type () => A.

When the expected type is known, Scala infers the parameter types, so List(1, 2, 3).map(x => x * 2) needs no annotation. For very short lambdas use the placeholder _:

SCALA
List(1, 2, 3).map(_ * 2)        // List(2, 4, 6)
List(1, 2, 3, 4).filter(_ > 2)  // List(3, 4)
List(3, 1, 2).sortBy(-_)        // List(3, 2, 1)

Each _ stands for the next parameter in order, so _ + _ is (a, b) => a + b. Use _ only when the lambda is tiny; otherwise name the parameter.

Methods and functions

A def is a method, a val holding => is a function value. Scala converts a method to a function automatically wherever a function is expected (this is called eta-expansion):

SCALA
def isEven(n: Int): Boolean = n % 2 == 0

val evens = List(1, 2, 3, 4, 5, 6).filter(isEven)   // List(2, 4, 6)
val f: Int => Boolean = isEven                       // method becomes a function value

The big three: map, filter, fold

Describe, do not loop

map Transform every element.
filter Keep elements that pass a test.
fold Combine all elements into one result.
flatMap Map, then flatten the results.
1 / 2
SCALA
val prices = List(12.5, 8.0, 30.0, 4.5)

val doubled = prices.map(_ * 2)                   // List(25.0, 16.0, 60.0, 9.0)
val expensive = prices.filter(_ > 10)             // List(12.5, 30.0)
val total = prices.foldLeft(0.0)(_ + _)           // 55.0
val longest = List("aa", "b", "cccc").maxBy(_.length)  // cccc

foldLeft(start)(combine) walks left to right, carrying an accumulator. Here the start is 0.0 and each step adds the next price. Fold is the general tool: map, filter, sum and max can all be written with it.

SCALA
// word counts with foldLeft
val words = List("a", "b", "a", "c", "a", "b")
val counts = words.foldLeft(Map.empty[String, Int]) { (acc, w) =>
  acc.updated(w, acc.getOrElse(w, 0) + 1)
}
println(counts)   // Map(a -> 3, b -> 2, c -> 1)

reduce is like fold without a start value and fails on an empty list; prefer foldLeft or sum when the list might be empty.

Functions that return functions

SCALA
def multiplier(factor: Int): Int => Int =
  n => n * factor

val triple = multiplier(3)
println(triple(5))         // 15
println(multiplier(10)(4)) // 40

triple is a closure: the returned lambda remembers the factor from the scope where it was created. Closures capture variables by reference, so a captured var shows later changes. Capturing only vals avoids surprises.

Composition

Small functions combine into bigger ones with andThen and compose:

SCALA
val trim: String => String = _.trim
val shout: String => String = _.toUpperCase
val exclaim: String => String = _ + "!"

val pipeline = trim.andThen(shout).andThen(exclaim)
println(pipeline("  hello  "))   // HELLO!

val reverseFirst = shout.compose(trim)   // runs trim first, then shout

f.andThen(g) means "f, then g"; f.compose(g) means "g, then f". Scala 3 also offers the |> style through libraries, but andThen is in the standard library.

flowchart LR
    A["'  hello  '"] -->|trim| B["'hello'"]
    B -->|shout| C["'HELLO'"]
    C -->|exclaim| D["'HELLO!'"]

Currying and partial application

A curried function takes its arguments one at a time. In Scala you write multiple parameter lists:

SCALA
def volume(width: Int)(height: Int)(depth: Int): Int = width * height * depth

val tall = volume(2)(10)    // still needs depth
println(tall(3))            // 60

Calling with fewer parameter lists gives a new function (in Scala 3 the compiler converts automatically; in Scala 2 you wrote volume(2)(10) _). This is partial application: fixing some arguments to create a more specific function.

Multiple parameter lists are also used so a function-typed argument can go last and use block syntax, and so that type inference works left to right:

SCALA
def retry[A](times: Int)(action: => A): A =
  try action
  catch case _ if times > 1 => retry(times - 1)(action)

val value = retry(3) {
  println("trying...")
  42
}

Here action: => A is a by-name parameter: it is not evaluated when retry is called but each time the body uses it. That is why the retry can run the block again. Compare with a normal parameter, which is evaluated once before the call.

You can also convert between forms: f.curried turns (A, B) => C into A => B => C, and Function.uncurried reverses it.

Functions in other JVM languages

The ideas exist everywhere. These two examples are not Scala but run on our site: Kotlin lambdas and a closure counter, and Java's Function composition.

Kotlin Kotlin: map, filter, fold, closures (Kotlin, not Scala)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

Java Java: Function composition and partial application (Java, not Scala)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Common mistakes

  • Overusing _. _ + _ is fine; _.foo(_.bar) is confusing. Name the parameter.
  • reduce on an empty collection throws. Use foldLeft with a start value.
  • Mutating a captured var inside a closure that runs in several threads.
  • Confusing andThen and compose (order of application).
  • Doing side effects inside map. Use foreach when you only want effects.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the type of (a: Int, b: Int) => a + b?

    It is a function from two Ints to an Int.
  2. 2. What does List(1, 2, 3).foldLeft(10)(_ + _) return?

    Fold starts from 10 and adds 1, 2 and 3 to it, giving 16.
  3. 3. What is triple in def multiplier(f: Int): Int => Int = n => n * f; val triple = multiplier(3)?

    The returned lambda captures f from its defining scope, which makes it a closure.
  4. 4. What does the => A in def retry[A](times: Int)(action: => A) mean?

    A by-name parameter is re-evaluated at each use, which is what lets retry run the block again.
  5. 5. Which expression runs trim first and shout second?

    f.andThen(g) applies f first and then g.

Summary

  • Functions are values with types like Int => Int; lambdas use => and short forms use _.
  • map, filter, flatMap and foldLeft replace most loops.
  • Functions can return functions; the result is a closure over its defining scope.
  • Compose with andThen/compose; curry with multiple parameter lists and use by-name parameters for lazily evaluated blocks.

Next: Option, Either and Try