n-ary reduce operator with support for prefix/suffix elements. Intermediate results are yielded as well. Like `functools.reduce` but more powerful.
Project description
reduce-ex
n-ary reduce operator with support for prefix/suffix elements. Intermediate results are yielded as well. Like functools.reduce but more powerful.
Key Features
- N-ary operation support: Works with functions of any arity (n ≥ 2)
- Intermediate results yielded: Not just the last result is returned
- Flexible input: Supports prefix/suffix elements for initialization/termination
- Memory efficient: Uses bounded deque for argument handling
- Zero Dependencies: Works with stock Python 2+
Usage
Basic Usage
For binary operations (n=2), reduce_ex maintains identical left-associative evaluation order as functools.reduce, while yielding intermediate results:
from reduce_ex import reduce_ex
for partial_sum in reduce_ex(lambda a, b: a + b, range(1, 5)):
print(partial_sum)
# Computation steps:
# 1+2 = 3
# 3+3 = 6
# 6+4 = 10
# Yields: 3, 6, 10
N-ary Operation (n=3)
For n-ary operations, the evaluation proceeds via left-associative sliding windows:
from reduce_ex import reduce_ex
# Polynomial recurrence: a * b + c
for partial_result in reduce_ex(lambda a, b, c: a * b + c, [2, 3, 4, 5, 6], n=3):
print(partial_result)
# Computation steps:
# 2 * 3 + 4 = 10
# 10 * 5 + 6 = 56
# Yields: 10, 56
With prefix
from math import tanh
from reduce_ex import reduce_ex
W_h = 0.5
W_x = 0.5
B = 0.5
# Simulate an RNN cell
def rnn_cell(h_prev, x):
global W_h, W_x, B
return tanh(W_h * h_prev + W_x * x + B)
h_0 = 0.0
for i, h_i in enumerate(
reduce_ex(
rnn_cell,
[0.1, 0.2, 0.3],
prefix=[h_0]
),
start=1
):
print(f"Hidden state {i}: {h_i}")
With prefix and suffix
from reduce_ex import reduce_ex
for partial_sum in reduce_ex(lambda a, b: a + b, range(5), prefix=(1, 2), suffix=(10,)):
print(partial_sum)
# Computation steps:
# 1 + 2 = 3
# 3 + 0 = 3
# 3 + 1 = 4
# 4 + 2 = 6
# 6 + 3 = 9
# 9 + 4 = 13
# 13 + 10 = 23
# Yields: 3, 3, 4, 6, 9, 13, 23
How It Works
The reducer maintains the following invariants:
- Consumes
nelements. - Calls function, yields result, uses the previous result as first argument, and consumes
(n-1)more elements. - Calls function, yields result, uses the previous result as first argument, and consumes
(n-1)more elements. - ...
API Reference
reduce_ex(function, iterable, n=2, prefix=(), suffix=())
function: Callable of arity niterable: Input elementsn: Operation arity (must be ≥ 2)prefix: Elements prepended to inputsuffix: Elements appended to input
Contributing
Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.
License
This project is licensed under the MIT License.
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 reduce_ex-0.1.0a0.tar.gz.
File metadata
- Download URL: reduce_ex-0.1.0a0.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aeccc6d43e165116c4d7a065a5929c59886bbc0ef88737d5b24b9063aff84f85
|
|
| MD5 |
d7ea947a3849f245bd4ebc9497473514
|
|
| BLAKE2b-256 |
3d3b700a3d06275bdecc5c670d295902b7f5858d1f5b8f32783d5983634d5390
|
File details
Details for the file reduce_ex-0.1.0a0-py2.py3-none-any.whl.
File metadata
- Download URL: reduce_ex-0.1.0a0-py2.py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
41ee4160ca1fd218576158b60e10d5435b60489ab775770cad1de32a4f7bdcb4
|
|
| MD5 |
4156dda3e8f21cbdc738c3b19fec682d
|
|
| BLAKE2b-256 |
3c4060935f4bcac94aa358046eb8f01818612c2cbfaaca695837a496659638c7
|