Functional Programming Patterns
-
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.
-
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.
-
Left and Right Folds
A fold combines values using an operation and a seed. Left and right folds group those applications differently.
-
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.
-
Currying a Function with Two Arguments
Currying expresses an operation on several arguments as a chain of functions, each taking one argument.
-
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.
-
Suspension in a Generator Function
A generator produces values over successive advances, preserving local state between yields when the language supports this model.
-
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.
-
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.
-
The Order of Filtering and Mapping
Filtering before a transformation asks about original values. Filtering afterward asks about transformed values.
-
Transforming an Optional Value
An optional mapping applies an operation only when a value is present and carries absence through the result.
-
Composing Operations with Success and Error Results
A result carrying success or an error can determine whether the next computation should receive a value.
-
Creating an Updated Copy of a Value
An immutable update creates a new value representing the change while older references retain the earlier state.
-
Structural Sharing Between Immutable Values
Structural sharing lets new and old immutable values reference unchanged parts while allocating only what differs.
-
Referential Transparency by Substitution
An expression is referentially transparent in a specified context when replacing it with its value preserves observable behavior.
-
Types Between Functional Pipeline Stages
The output of a pipeline stage must contain the representation and guarantees consumed by the next stage.
-
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.
-
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.
-
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.
-
Associative Operations for Combining Summaries
An associative operation lets adjacent inputs be regrouped without changing the result, so partitions can often be summarized separately.
-
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.
-
Composing Small Text Formatters
Separate value formatting, escaping, and record layout according to the target representation expected at each stage.
-
Lazily Enumerating Combinations of Two Inputs
An iterator can produce ordered pairs on demand without materializing the full Cartesian product.
-
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.
-
Consuming a Lazy Sequence at an Expected Boundary
An operation consuming a lazy sequence determines when its work, effects, and failures actually occur.
-
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.
-
Reusing Pure Calculations by Memoization
A pure computation with stable input representations can reuse a stored result for equivalent arguments.
-
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.
-
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.
-
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.