# Functional Patterns

## Overview

Functional patterns leverage concepts from functional programming to write code that is more predictable, composable, and testable. These patterns emphasize immutability, pure functions, and declarative data transformations.

## Patterns in This Category

| Pattern | Purpose | Key Mechanism |
|---------|---------|---------------|
| [Higher-Order Functions](higher-order-functions.md) | Pass and return functions as values | map, filter, reduce |
| [Monad](monad.md) | Chain operations with context (errors, nulls) | Maybe, Result types |
| [Immutability](immutability.md) | Prevent state mutation after creation | Frozen / read-only objects |
| [Currying](currying.md) | Transform multi-arg functions into chains | Partial application |
| [Memoization](memoization.md) | Cache function results by inputs | Lookup table / decorator |
| [Pattern Matching](pattern-matching.md) | Destructure and branch on data shape | match/case, type switch |
| [Lazy Evaluation](lazy-evaluation.md) | Defer computation until value is needed | Generators, thunks |

## When to Use Functional Patterns

- **Data pipelines**: Higher-Order Functions, Lazy Evaluation
- **Error handling without exceptions**: Monad (Result/Maybe)
- **Concurrent / parallel code**: Immutability (no shared mutable state)
- **Configuration and DSLs**: Currying
- **Performance optimization**: Memoization, Lazy Evaluation
- **Complex conditional logic**: Pattern Matching

## General Principles

1. **Prefer pure functions** -- same input always produces same output, no side effects.
2. **Avoid mutation** -- create new values instead of modifying existing ones.
3. **Compose small functions** into larger transformations.
4. **Make illegal states unrepresentable** using types (Monad pattern).
5. **Defer work** until actually needed (Lazy Evaluation).
