A Python library for functional programming that enables elegant function composition (chaining).
This project demonstrates how to implement and use function composition in Python, allowing you to create more readable, maintainable, and flexible data processing pipelines. By using the @composable decorator, regular functions can be transformed into composable functions that can be chained together in a clean, functional programming style.
- Convert regular functions into composable functions with a simple decorator
- Chain multiple operations together in a readable syntax
- Create reusable data processing pipelines
- Improve code readability and maintainability
- Enhance flexibility in function execution order
- Facilitate unit testing by allowing functions to be tested in isolation before composition
sample_data/- Data files in CSV and JSON format for demonstrationmodel.py- Pydantic models representing the records in the sample data fileslibrary.py- A library of single-responsibility functions for processing filescomposable.py- A decorator for converting regular functions into composable functionscomposable_library.py- The same library functions as inlibrary.pybut decorated with the@composabledecoratordemo_1.py- Demonstration of using the functions in a traditional waydemo_2.py- Demonstration of using composable functions in a chained mannerdemo_3.py- Demonstration of the flexibility of composable functionsdemo_4.py- Demonstration of the durability and reusability of composable functions
- Python 3.6+ (developed with Python 3.12)
- pipenv (install with
pip install pipenvif not already installed)
-
Clone the repository:
git clone https://github.com/yourusername/composable.git cd composable -
Create a virtual environment and install dependencies:
pipenv install
-
Activate the virtual environment:
pipenv shell
Run the demo scripts to see function composition in action:
python demo_1.py # Traditional function usage
python demo_2.py # Using composable functions
python demo_3.py # Flexibility of composable functions
python demo_4.py # Reusability of composable functionsTraditional approach:
result = function3(function2(function1(data)))With composable functions:
result = data | function1 | function2 | function3Contributions are welcome! Please feel free to submit a Pull Request.
This project is available under the MIT License - see the LICENSE file for details.
Originally created by Steve Brettschneider
- LinkedIn: www.linkedin.com/in/stevebrettschneider
- Email: steve.brettschneider@pinnsg.com
- Company: Pinnacle Solutions Group
This project was initially presented at CincyPy on May 29, 2025.
- Blog post: Function Composition in Python
- LinkedIn article: Function Composition in Python - LinkedIn Post
This code is provided "as is" without warranty of any kind, express or implied. The author makes no warranties or guarantees about this code and is not responsible for any damage or issues that may occur from using it. Use at your own risk.
