Functional Programming Patterns

  1. Applying Two Transformations in One Pass

    Two item-by-item transformations can be composed into one pass when they do not depend on separate passes or their observable effects.

    Undergraduate
  2. Combining Filters into One Predicate

    Two successive order-preserving filters can select the same values as a conjunction when the tests are independent of effects and traversal history.

    Undergraduate
  3. Left and Right Folds

    A fold combines values using an operation and a seed. Left and right folds group those applications differently.

    Undergraduate
  4. Fixing an Argument to Create a New Function

    Partial application creates a callable by fixing one or more inputs of an existing function while leaving other inputs for later.

    Undergraduate
  5. Currying a Function with Two Arguments

    Currying expresses an operation on several arguments as a chain of functions, each taking one argument.

    Undergraduate
  6. Closures Built in a Loop

    A closure can retain access to a binding whose value changes. Several closures created in a loop may therefore observe one later value.

    Undergraduate
  7. Suspension in a Generator Function

    A generator produces values over successive advances, preserving local state between yields when the language supports this model.

    Undergraduate
  8. Lazy and Eager Transformations

    An eager transform computes its results before returning them. A lazy transform can delay each computation until the result is requested.

    Undergraduate
  9. Reading a Pipeline of Iterators

    A generator pipeline passes values through stages as consumers request them. Each stage supplies a documented transformation or filtering rule.

    Undergraduate
  10. The Order of Filtering and Mapping

    Filtering before a transformation asks about original values. Filtering afterward asks about transformed values.

    Undergraduate
  11. Transforming an Optional Value

    An optional mapping applies an operation only when a value is present and carries absence through the result.

    Undergraduate
  12. Composing Operations with Success and Error Results

    A result carrying success or an error can determine whether the next computation should receive a value.

    Undergraduate
  13. Creating an Updated Copy of a Value

    An immutable update creates a new value representing the change while older references retain the earlier state.

    Undergraduate
  14. Structural Sharing Between Immutable Values

    Structural sharing lets new and old immutable values reference unchanged parts while allocating only what differs.

    Undergraduate
  15. Referential Transparency by Substitution

    An expression is referentially transparent in a specified context when replacing it with its value preserves observable behavior.

    Undergraduate
  16. Types Between Functional Pipeline Stages

    The output of a pipeline stage must contain the representation and guarantees consumed by the next stage.

    Undergraduate
  17. A Pure Calculation Inside an Effectful Program

    A program can acquire data and publish results at its edges while a predictable core computes from explicit inputs.

    Undergraduate
  18. Reducing a Sequence of Events to State

    A reducer computes a next state from a current state and one event. Replaying an ordered event sequence can reconstruct a result.

    Undergraduate
  19. Local Map and Reduce with Counts

    A map can transform observations into intermediate contributions and a reduction can combine those contributions under a stated grouping rule.

    Undergraduate
  20. Associative Operations for Combining Summaries

    An associative operation lets adjacent inputs be regrouped without changing the result, so partitions can often be summarized separately.

    Undergraduate
  21. Choosing an Identity for a Fold

    An identity leaves an operand unchanged when combined with it. It often supplies a useful meaning for folding an empty collection.

    Undergraduate
  22. Composing Small Text Formatters

    Separate value formatting, escaping, and record layout according to the target representation expected at each stage.

    Undergraduate
  23. Lazily Enumerating Combinations of Two Inputs

    An iterator can produce ordered pairs on demand without materializing the full Cartesian product.

    Undergraduate
  24. Mapping Each Input to Zero or More Results

    Flat mapping applies a function returning a sequence to each input and concatenates the resulting sequences in visitation order.

    Undergraduate
  25. Consuming a Lazy Sequence at an Expected Boundary

    An operation consuming a lazy sequence determines when its work, effects, and failures actually occur.

    Undergraduate
  26. Comparing Items or Computing Sort Keys

    A comparator directly decides the order of a pair. A key function maps a single item to a value whose comparison determines that order.

    Undergraduate
  27. Reusing Pure Calculations by Memoization

    A pure computation with stable input representations can reuse a stored result for equivalent arguments.

    Undergraduate
  28. Preserving Order in a Reduction

    Associativity permits regrouping, while commutativity permits reordering. A reduction can require the operand order even when it uses many partitions.

    Undergraduate
  29. Keeping One Input Snapshot for a Transformation

    A transformation applied across many records can use one captured configuration when all results are meant to be comparable.

    Undergraduate
  30. Testing a Composed Transformation End to End

    Individual tests establish what every stage promises. A composed test checks the actual representations handed from one stage to another.

    Undergraduate