Skip to content

sajonaro/python-tutorial-func

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Functional Programming Guide

A practical guide to functional programming concepts in Python, organized into dedicated modules for easy learning and reference.

TL;DR - Quick Start

Run all demonstrations:

bash run.sh

This will build a Docker container and execute all functional programming demonstrations.

Modules Description

1. immutability.py - Immutable Data Structures

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.

2. map_filter_reduce.py - Map, Filter, Reduce

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.

3. lambdas.py - Lambda Expressions

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.

4. currying.py - Currying and Partial Application

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.

5. higher_order_functions.py - Higher-Order Functions

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.

6. transducers.py - Transducers

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.

7. monads.py - Monads

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.

Running the Project

Run All Demonstrations

bash run.sh

Run Individual Modules

Each 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.monads

Run via Main Entry Point

python main.py

Key Takeaways

  1. IMMUTABILITY: Use tuples, frozenset, namedtuple, or dataclass(frozen=True)
  2. MAP/FILTER/REDUCE: List comprehensions often preferred in Python
  3. LAMBDAS: Best for short, simple expressions
  4. CURRYING/PARTIAL: functools.partial() is pythonic
  5. HIGHER-ORDER FUNCTIONS: Very pythonic! Decorators are HOFs
  6. TRANSDUCERS: Not idiomatic; use itertools instead
  7. MONADS: Not idiomatic, but useful patterns exist

Recommendation

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.

Requirements

  • Python 3.11+
  • Docker (for containerized execution)
  • UV package manager (automatically installed in Docker)

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published