Functional programming for Python
Project description
pipette
pipette is a lightweight, expressive Python library designed to enable clean, functional-style data processing pipelines with intuitive syntax and powerful composition.
Note that due to the limitations of Python's static typing, some features may not provide type hints as expected. However, the library is designed to be flexible and easy to use, even without strict type enforcement.
Features
pipette is currently in early development, but it already includes:
- Lazy evaluation for efficient data processing.
- Support for custom transformations using the
@pipettedecorator. - Core functional utilities like
select,where,reduce,sort_by, and more. - Currying support for partial function application.
- Basic monad implementation for chaining operations with error handling.
Installation
pipette can be installed via pip from the Python Package Index (PyPI):
pip install pipette-fp
Alternatively, you can install the latest development version of pipette directly from GitHub:
pip install git+https://github.com/chriso345/pipette
Usage
pipette aims to make functional pipelines clear and concise.
from pipette import where, select, into
data = [
{"active": True, "value": 10},
{"active": False, "value": 5},
{"active": True, "value": 7},
]
result = (
data
| where(lambda x: x["active"])
| select(lambda x: x["value"])
| into(list)
)
print(result) # Output: [10, 7]
Custom transformations can easily be added to extend functionality:
from pipette import pipette
@pipette
def double(x):
return builtins.map(lambda n: n * 2, x)
result = (
[1, 2, 3, 4]
| double
| into(list)
)
print(result) # Output: [2, 4, 6, 8]
Curry
pipette provides a submodule for easy currying of functions:
from pipette.curry import curry
@curry
def add(x: int, y: int) -> int:
return x + y
result = add(2)(3)
print(result) # Output: 5
result = add(2)
print(result(3)) # Output: 5
Note, however, that python's static typing does not support currying and the type hints will error.
Monads
pipette also includes a simple monad implementation for chaining operations:
from pipette.monad import Maybe, Some, Nothing
def safe_divide(x, y) -> Maybe[float]:
if y == 0:
return Nothing()
else:
return Some(x / y)
s = Some(10) >> (lambda x: safe_divide(x, 2)) | (lambda x: x + 1)
print(s) # Output: Some(6.0)
n = Some(10) >> (lambda x: safe_divide(x, 0)) | (lambda x: x + 1)
print(n) # Output: Nothing
Here >> binds the monad value to the function, and | provides a simple way to map the result to another function.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pipette_fp-1.0.0.tar.gz.
File metadata
- Download URL: pipette_fp-1.0.0.tar.gz
- Upload date:
- Size: 8.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e1afecb0eb8555a298e4eb27957c18a33ca8a3729124a681da5ffa40d5e96abf
|
|
| MD5 |
12a832155d7bc97bdbb2bde366f8b2e6
|
|
| BLAKE2b-256 |
4fc658987766a51ba9c71030b330ea6a31c414d5648472a0dd8f1bd59b67a57e
|
File details
Details for the file pipette_fp-1.0.0-py3-none-any.whl.
File metadata
- Download URL: pipette_fp-1.0.0-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8a2006f86d63be63fe22c36e0a2564b3066180cabe6caeb083d0cab9010b540d
|
|
| MD5 |
20d8403fa1a0e01866448a84db88947a
|
|
| BLAKE2b-256 |
a655a7893c2075cb816e5dfdb970caac9d6690c6a065d07af161a610b605ce88
|