liveBook · Manning

Pure function

As explained in Fabulous Adventures in Data Structures and Algorithms

A pure function is a function whose result depends only on its input values and that has no observable interaction with global state.

A pure function produces an output determined only by its input values. It neither reads nor changes global state, making the same input reliably produce the same result. section 4.3.4

That predictability enables memoization: cache the first result for each input and return the cached result on later calls. Immutable arguments are required so cached results remain valid. section 4.3.4 section 10.2

Purity has practical consequences:

  • Memoization trades additional memory for less computation when repeated inputs occur. section 4.4.4
  • A cache needs lifecycle controls because low-value entries can consume substantial memory; clearing or replacing the dictionary limits that cost. section 4.3.4
  • Real measurements matter: a small number of frequently reused entries may provide most of the benefit while many entries remain cold. section 4.4.4

Memoizing a pure function

A one-argument memoizer stores each newly computed result in a dictionary, then returns that result for subsequent calls with the same input. The pattern applies to pure operations such as constructing or stepping immutable quadtrees. section 4.3.4 section 4.4.4

Listing 4.3 A general-purpose one-argument memoizer
sealed class Memoizer<A, R> where A : notnull
{
    private Dictionary<A, R> dict;
    private readonly Func<A, R> f;
    public Memoizer(Func<A, R> f)
    {
        this.dict = new Dictionary<A, R>();
        this.f = f;
    }
    public R MemoizedFunc(A a)
    {
        if (!dict.TryGetValue(a, out R? r))
        {
            r = f(a);
            dict.Add(a, r);
        }
        return r;
    }
    public void Clear(Dictionary<A, R>? newDict = null)
    {
        dict = newDict ?? new Dictionary<A, R>();
    }
    public int Count => dict.Count;
}

Inspect the complete C# Memoizer implementation, including dictionary lookup, cache insertion, clearing, and entry counting. section 4.3.4

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Defines purity, demonstrates a generic memoizer, and measures cache behavior without detailing a formal effect system.
Examples
Uses a C# memoizer, immutable quadtrees, HashLife's Step method, and memoized term-processing operations.
What it assumes
Assumes familiarity with functions and immutable values while defining purity and its memoization consequences directly.
What you can do after
Apply it via memoization by identifying repeatable inputs, caching results, and monitoring or clearing cache growth.
How it connects
Connects pure functions to memoization, immutable data structures, HashLife, and unification.

Also explained in

Compare how 19 books explain pure function →

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.