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
- 🚀 Multi-Backend Performance: 5 backends for optimal performance
- Python: Universal compatibility (always available)
- C++: High-performance general operations
- Rust: Memory-safe statistical functions
- Go: Lightning-fast bitwise operations
- Zig: Blazing mathematical computations (236x speedup!)
🚀 Quick Start
pip install pyfunc-pipeline
from pyfunc import pipe, _
result = pipe([1, 2, 3, 4]).filter(_ > 2).map(_ * 10).to_list()
result = pipe("hello world").explode(" ").map(_.capitalize()).implode(" ").get()
double = _ * 2
square = _ ** 2
composed = double >> square
result = pipe(5).apply(composed).get()
⚡ Performance Backends
from pyfunc import pipe, set_zig_threshold, set_go_threshold
set_zig_threshold(1000)
set_go_threshold(500)
large_data = list(range(5000))
result = pipe(large_data).sum().get()
result = pipe([1, 2, 3, 4, 5]).sum_zig().get()
result = pipe([15, 31, 63]).bitwise_and_go(7).to_list()
result = pipe([1, 2, 3, 4, 5]).median_rust().get()
from pyfunc.backends import get_backend
backend = get_backend()
if backend.zig_backend:
stats = backend.zig_backend.batch_statistics([1, 2, 3, 4, 5])
🎯 Core Concepts
Pipeline Chaining
Every value can be lifted into a pipeline for transformation:
from pyfunc import pipe, _
pipe([1, 2, 3, 4]).filter(_ > 2).map(_ ** 2).sum().get()
pipe(" hello world ").apply(_.strip().title()).explode(" ").to_list()
pipe({"a": 1, "b": 2}).map_values(_ * 10).get()
Placeholder Syntax
The _ placeholder creates reusable, composable expressions:
from pyfunc import _
double = _ * 2
add_ten = _ + 10
normalize = _.strip().lower()
is_positive = _ > 0
process = double >> add_ten
Lazy Evaluation
Operations are lazy by default - perfect for large datasets:
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()
pipe("Hello {name}!").template_fill({"name": "PyFunc"}).get()
Dictionary Operations
users = {"alice": 25, "bob": 30}
pipe(users).map_values(_ + 5).map_keys(_.title()).get()
Advanced Transformations
data = [{"name": "Alice", "dept": "Eng"}, {"name": "Bob", "dept": "Sales"}]
pipe(data).group_by(_["dept"]).get()
pipe([1, 2, 3, 4, 5]).window(3).to_list()
pipe([1, 2, 3]).combinations(2).to_list()
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, _
orders = [
{"id": 1, "customer": "Alice", "items": ["laptop", "mouse"], "total": 1200.50},
{"id": 2, "customer": "Bob", "items": ["keyboard"], "total": 75.00},
{"id": 3, "customer": "Charlie", "items": ["monitor", "stand"], "total": 450.25}
]
result = (
pipe(orders)
.filter(_["total"] > 100)
.map({
"id": _["id"],
"customer": _["customer"],
"discounted_total": _["total"] * 0.9
})
.map("Order #{id} for {customer}: ${discounted_total:.2f}")
.to_list()
)
print(result)
⚠️ Important Note
Use regular string templates, not f-strings:
.map(f"Order #{_['id']}")
.map("Order #{id}")
📖 Documentation
🚀 Performance Backends
🔧 Backend Installation
pip install pyfunc-pipeline[all]
pip install pyfunc-pipeline[cpp]
pip install pyfunc-pipeline[zig]
pip install pyfunc-pipeline[rust]
pip install pyfunc-pipeline[go]
python build_zig.py
python build_go.py
python build_cpp.py
🤝 Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
📄 License
MIT License - see LICENSE file for details.