liveBook · Manning

Partial application

As explained in Fabulous Adventures in Data Structures and Algorithms

Partial application is the creation of a function by binding one or more arguments of another function to particular values.

Partial application creates a function by binding one or more arguments of another function to particular values. For a two-argument function, binding the left argument produces a one-argument function that remembers the bound value. Currying instead transforms a multi-argument function into nested one-argument functions; partial application can then specialize one of them. section 2.7.2

Binding an adder

A curried adder turns `x` into a function waiting for `y`: `Func<int, Func<int, int>> curried = x => y => adder(x, y);`. Applying `curried(4)` creates `add4`, which adds four to its argument. More unexpectedly, invoking the resulting function with `0` retrieves the bound value: `curried(123)(0)` produces `123`. section 2.7.2

The same idea can represent a Hughes list. Instead of storing elements directly, the list stores a partially applied concatenation function. Construction stays cheap while concatenation composes functions; converting the structure to a stack invokes the function and pays the deferred cost. section 2.7.3

In the Hughes-list implementation, partial application appears as:

  • A `Concat` delegate accepting a stack and returning a stack. section 2.7.3
  • Lambdas that capture existing stacks or composed list functions. section 2.7.3
  • `ToStack`, which supplies the empty stack and forces the accumulated computation. section 2.7.3

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Explains binding, currying, captured values, and deferred Hughes-list concatenation through C# listings, but does not quantify performance.
Examples
Demonstrates a curried adder, retrieving a bound value with zero, and a Hughes list backed by partially applied concatenation.
What it assumes
Assumes familiarity with C# delegate types and lambdas while illustrating their use through small function examples.
What you can do after
Apply it via C# delegates to specialize functions and defer concatenation until a Hughes list is converted to a stack.
How it connects
Connects partial application to currying, delegates, immutable stacks, lazy evaluation, and Hughes lists.

Also explained in

Compare how 10 books explain partial application →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fabulous Adventures in Data Structures and Algorithms. The section links point to the parts of the book it draws on.