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

Partial Application

In this page:

  1. Partial Application

Partial Application

Partial application fixes some of a function's arguments upfront, producing a new function that only needs the remaining ones, using the _ placeholder for unfixed positions, like add(5, _: Int). This is closely related to currying but works on ordinary (non-curried) multi-parameter functions too. It's a way to specialize a general function into a more specific one without writing new code.

Note: Partial application with _ works on regular multi-parameter functions, not just curried ones with multiple parameter lists.

Example: Partial Application

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

  val addFive: Int => Int = add(5, _: Int)

  println(addFive(10))
  println(addFive(20))
}
🔒

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.