This release is a pre-release and may not be stable for production use.
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.
Release files for reduce-ex 0.1.0a0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| reduce_ex-0.1.0a0.tar.gz | 3.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| reduce_ex-0.1.0a0-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 7.5 kB
Release files / reduce_ex-0.1.0a0.tar.gz
| Download URL | reduce_ex-0.1.0a0.tar.gz |
|---|---|
| Size | 3.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
aeccc6d43e165116c4d7a065a5929c59886bbc0ef88737d5b24b9063aff84f85
|
|
BLAKE2b-256 checksum How to use checksums |
3d3b700a3d06275bdecc5c670d295902b7f5858d1f5b8f32783d5983634d5390
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.2
|
Release files / reduce_ex-0.1.0a0-py2.py3-none-any.whl
| Download URL | reduce_ex-0.1.0a0-py2.py3-none-any.whl |
|---|---|
| Size | 3.9 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
41ee4160ca1fd218576158b60e10d5435b60489ab775770cad1de32a4f7bdcb4
|
|
BLAKE2b-256 checksum How to use checksums |
3c4060935f4bcac94aa358046eb8f01818612c2cbfaaca695837a496659638c7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.2
|