A practical guide to functional programming concepts in Python, organized into dedicated modules for easy learning and reference.
Run all demonstrations:
bash run.shThis will build a Docker container and execute all functional programming demonstrations.
Learn how to work with immutable data in Python:
- Tuples: Immutable sequences
- Frozenset: Immutable sets
- Namedtuple: Immutable objects with named fields
- Frozen Dataclass: Immutable dataclasses (Python 3.7+)
- MappingProxyType: Immutable dictionaries
Key Takeaway: Python isn't purely functional, so immutability requires discipline.
Functional transformations and reductions:
- Map: Transform each element in a collection
- Filter: Select elements matching a predicate
- Reduce: Accumulate values (fold operation)
- Pythonic alternatives using list comprehensions
Key Takeaway: List comprehensions are often preferred in Python for readability.
Using anonymous functions effectively:
- Basic lambda syntax
- Lambdas in sorting operations
- Chaining lambdas with map/filter
- Best practices and limitations
Key Takeaway: Use lambdas for short, simple expressions; use def for complex logic.
Function transformation techniques:
- Manual Currying: Converting multi-argument functions to single-argument chains
- Partial Application: Pre-filling function arguments with
functools.partial - General curry function implementation
Key Takeaway: functools.partial() is the pythonic way to handle partial application.
Functions as first-class citizens:
- Functions as arguments
- Functions as return values
- Function Composition: Combining functions
- Decorators: Pythonic higher-order functions
- Memoization: Caching function results
Key Takeaway: Very pythonic! Decorators are HOFs and widely used in Python.
Composable algorithmic transformations:
- Traditional multi-pass vs. single-pass transformations
- Transducer pattern implementation
- Pythonic alternatives with
itertools
Key Takeaway: Not idiomatic in Python; use itertools for lazy, composable transformations.
Handling context in a composable way:
- Maybe/Option Monad: Handle None/null safely
- Either/Result Monad: Error handling without exceptions
- Function pipelines
- Pythonic alternatives
Key Takeaway: Not idiomatic in Python, but useful patterns for specific use cases.
bash run.shEach module can be run independently:
python -m modules.immutability
python -m modules.map_filter_reduce
python -m modules.lambdas
python -m modules.currying
python -m modules.higher_order_functions
python -m modules.transducers
python -m modules.monadspython main.py- IMMUTABILITY: Use tuples, frozenset, namedtuple, or dataclass(frozen=True)
- MAP/FILTER/REDUCE: List comprehensions often preferred in Python
- LAMBDAS: Best for short, simple expressions
- CURRYING/PARTIAL:
functools.partial()is pythonic - HIGHER-ORDER FUNCTIONS: Very pythonic! Decorators are HOFs
- TRANSDUCERS: Not idiomatic; use itertools instead
- MONADS: Not idiomatic, but useful patterns exist
Use Python's functional features where they improve clarity, but don't force purely functional patterns. Python is multi-paradigm - embrace comprehensions, generators, and itertools for pythonic functional style.
- Python 3.11+
- Docker (for containerized execution)
- UV package manager (automatically installed in Docker)
MIT