Skip to content

CincyPy/composable-functions

Repository files navigation

logo

Function Composition in Python

A Python library for functional programming that enables elegant function composition (chaining).

Overview

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.

Features

  • 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

Project Structure

  • sample_data/ - Data files in CSV and JSON format for demonstration
  • model.py - Pydantic models representing the records in the sample data files
  • library.py - A library of single-responsibility functions for processing files
  • composable.py - A decorator for converting regular functions into composable functions
  • composable_library.py - The same library functions as in library.py but decorated with the @composable decorator
  • demo_1.py - Demonstration of using the functions in a traditional way
  • demo_2.py - Demonstration of using composable functions in a chained manner
  • demo_3.py - Demonstration of the flexibility of composable functions
  • demo_4.py - Demonstration of the durability and reusability of composable functions

Installation

Prerequisites

  • Python 3.6+ (developed with Python 3.12)
  • pipenv (install with pip install pipenv if not already installed)

Setup

  1. Clone the repository:

    git clone https://github.com/yourusername/composable.git
    cd composable
  2. Create a virtual environment and install dependencies:

    pipenv install
  3. Activate the virtual environment:

    pipenv shell

Usage

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 functions

Example

Traditional approach:

result = function3(function2(function1(data)))

With composable functions:

result = data | function1 | function2 | function3

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is available under the MIT License - see the LICENSE file for details.

Author

Originally created by Steve Brettschneider

Acknowledgments

This project was initially presented at CincyPy on May 29, 2025.

Additional Resources

Disclaimer

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.

About

A demo of composable functions in Python.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages