liveBook · Manning

Partial application

As explained in The Coder Cafe

A partially applied function is a function produced by fixing some arguments of another function, leaving fewer arguments to supply.

Partial application creates a new function by fixing some arguments of an existing function, leaving fewer arguments to provide. It specializes a general operation for reuse. section 3.1

Partial application and currying manipulate functions differently:

  • Partial application fixes selected arguments, producing a function with fewer parameters. section 3.1.2
  • Currying transforms a multi-argument function into a sequence of single-argument functions. section 3.1.1
  • Combining currying with partial application can make specialization more concise than placeholder syntax. section 3.1.1

Specializing a three-argument addition function

Given add(x, y, z), fixing x to 5 produces addFive, which accepts only y and z. Calling addFive(2, 3) supplies the remaining arguments and returns 10. section 3.1

def add(x: Int, y: Int, z: Int): Int = x + y + z

The listing provides the original three-argument function that partial application specializes. section 3.1

How The Coder Cafe covers it

How deep it goes
Defines argument fixing and demonstrates it with addition, while distinguishing the technique from currying.
Examples
Uses a three-integer addition function, addFive, placeholders, and the call addFive(2, 3).
What you can do after
Build it from scratch by fixing selected arguments and reusing the resulting function with the remaining inputs.
How it connects
Connects partial application to currying, function composition, and functional programming.

Also explained in

Compare how 13 books explain partial application →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Coder Cafe. The section links point to the parts of the book it draws on.