Skip to main content

Functional programming pipeline for Python with chainable operations, lazy evaluation, and elegant placeholder syntax

Project description

PyFunc: Functional Programming Pipeline for Python

PyFunc is a Python library that brings functional programming fluency to Python, enabling chainable, composable, lazy, and debuggable operations on various data structures.

✨ Features

  • 🔗 Chainable Operations: Method chaining for readable data transformations
  • 🎯 Placeholder Syntax: Use _ to create lambda-free expressions
  • ⚡ Lazy Evaluation: Operations computed only when needed
  • 🔄 Function Composition: Compose functions with >> and << operators
  • 📊 Rich Data Operations: Works with scalars, lists, dicts, generators
  • 🐛 Built-in Debugging: Debug and trace pipeline execution
  • 🔧 Extensible: Register custom types and extend functionality
  • 📝 Type Safe: Full type hints and generic support

🚀 Quick Start

pip install pyfunc
from pyfunc import pipe, _

# Basic pipeline
result = pipe([1, 2, 3, 4]).filter(_ > 2).map(_ * 10).to_list()
# Result: [30, 40]

# String processing  
result = pipe("hello world").explode(" ").map(_.capitalize()).implode(" ").get()
# Result: "Hello World"

# Function composition
double = _ * 2
square = _ ** 2
composed = double >> square  # square(double(x))

result = pipe(5).apply(composed).get()
# Result: 100

🎯 Core Concepts

Pipeline Chaining

Every value can be lifted into a pipeline for transformation:

from pyfunc import pipe, _

# Numbers
pipe([1, 2, 3, 4]).filter(_ > 2).map(_ ** 2).sum().get()
# Result: 25

# Strings  
pipe("  hello world  ").apply(_.strip().title()).explode(" ").to_list()
# Result: ['Hello', 'World']

# Dictionaries
pipe({"a": 1, "b": 2}).map_values(_ * 10).get()
# Result: {"a": 10, "b": 20}

Placeholder Syntax

The _ placeholder creates reusable, composable expressions:

from pyfunc import _

# Arithmetic operations
double = _ * 2
add_ten = _ + 10

# Method calls
normalize = _.strip().lower()

# Comparisons  
is_positive = _ > 0

# Composition
process = double >> add_ten  # add_ten(double(x))

Lazy Evaluation

Operations are lazy by default - perfect for large datasets:

# Processes only what's needed from 1 million items
result = pipe(range(1_000_000)).filter(_ > 500_000).take(5).to_list()

📚 Rich API

String Operations

pipe("hello,world").explode(",").map(_.capitalize()).implode(" & ").get()
# "Hello & World"

pipe("Hello {name}!").template_fill({"name": "PyFunc"}).get()  
# "Hello PyFunc!"

Dictionary Operations

users = {"alice": 25, "bob": 30}
pipe(users).map_values(_ + 5).map_keys(_.title()).get()
# {"Alice": 30, "Bob": 35}

Advanced Transformations

# Group by
data = [{"name": "Alice", "dept": "Eng"}, {"name": "Bob", "dept": "Sales"}]
pipe(data).group_by(_["dept"]).get()

# Sliding windows
pipe([1, 2, 3, 4, 5]).window(3).to_list()
# [[1, 2, 3], [2, 3, 4], [3, 4, 5]]

# Combinations
pipe([1, 2, 3]).combinations(2).to_list()  
# [(1, 2), (1, 3), (2, 3)]

Side Effects & Debugging

pipe([1, 2, 3, 4])
    .debug("Input")
    .filter(_ > 2) 
    .debug("Filtered")
    .map(_ ** 2)
    .to_list()

🌟 Real-World Example

from pyfunc import pipe, _

# Process user data
users = [
    {"name": "  Alice  ", "age": 30, "scores": [85, 92, 78]},
    {"name": "BOB", "age": 25, "scores": [90, 88, 95]},
    {"name": "charlie", "age": 35, "scores": [75, 80, 85]},
]

top_performers = (
    pipe(users)
    .map(lambda user: {
        "name": pipe(user["name"]).apply(_.strip().title()).get(),
        "age": user["age"], 
        "avg_score": pipe(user["scores"]).sum().get() / len(user["scores"])
    })
    .filter(lambda user: user["avg_score"] > 80)
    .sort(key=lambda user: user["avg_score"], reverse=True)
    .to_list()
)

print(top_performers)
# [{'name': 'Bob', 'age': 25, 'avg_score': 91.0}, 
#  {'name': 'Alice', 'age': 30, 'avg_score': 85.0}]

📖 Documentation

🤝 Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines.

📄 License

MIT License - see LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyfunc_pipeline-0.2.0.tar.gz (18.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyfunc_pipeline-0.2.0-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file pyfunc_pipeline-0.2.0.tar.gz.

File metadata

  • Download URL: pyfunc_pipeline-0.2.0.tar.gz
  • Upload date:
  • Size: 18.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for pyfunc_pipeline-0.2.0.tar.gz
Algorithm Hash digest
SHA256 64c9430841b205a10f5c68353a8c12e10212cec6618bc75ea2fcffad23c73043
MD5 458b6ce8dbb76b277f48a833979496ef
BLAKE2b-256 d8ae72af0adcbace0f9c8498897ab0a54ccc55128158eef71352ae4610422e4f

See more details on using hashes here.

File details

Details for the file pyfunc_pipeline-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pyfunc_pipeline-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9521956bdd0f96d3e8c4e0a355dd70ef7f12deb46c8f554f085eaa6b6a389058
MD5 e02747dd87aa297829f6e0a42a6992a3
BLAKE2b-256 97bd00ba321708169b1987019b1199d3716f14370f40516bf8b50c15a0182de7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page