cytoolz Library
cytoolz provides a high-performance, Cython-implemented version of the functional utilities found in the toolz library. It offers drop-in replacements for many common `toolz` functions, primarily focusing on iteration and composition for speed improvements. The current version is 1.1.0. Release cadence generally follows significant updates or releases of the upstream `toolz` library.
Warnings
- gotcha Performance benefits are not universal. While `cytoolz` offers significant speedups for many iterative and compositional operations, particularly those involving loops, simple function calls or operations already highly optimized in CPython might not see major gains, and could even incur minor overhead due to C-Python bridge calls. Measure performance in your specific use case.
- gotcha `cytoolz`'s API mirrors `toolz`. `cytoolz` aims to be a drop-in replacement for `toolz` primitives. Therefore, users should consult the `toolz` documentation for specific API details (function signatures, available utilities). Changes in `toolz`'s API (e.g., removals, argument changes) will generally apply to `cytoolz` as well.
- gotcha `toolz` is a required runtime dependency. Despite `cytoolz` providing Cythonized versions of many `toolz` functions, the `toolz` library itself is an explicit and necessary dependency. Ensure both `cytoolz` and `toolz` are installed in your environment.
Install
-
pip install cytoolz
Imports
- compose
from cytoolz.functoolz import compose
- pipe
from cytoolz.functoolz import pipe
- map
from cytoolz.itertoolz import map
- groupby
from cytoolz.itertoolz import groupby
- get_in
from cytoolz.dicttoolz import get_in
Quickstart
from cytoolz.functoolz import compose, pipe
from cytoolz.itertoolz import map, filter
def add_one(x):
return x + 1
def multiply_by_two(x):
return x * 2
def is_even(x):
return x % 2 == 0
# Example 1: Compose functions
process = compose(multiply_by_two, add_one)
result_compose = process(5) # (5 + 1) * 2 = 12
print(f"Compose result: {result_compose}")
# Example 2: Pipe data through functions
result_pipe = pipe(5, add_one, multiply_by_two)
print(f"Pipe result: {result_pipe}")
# Example 3: Map and filter with cytoolz iterators
numbers = [1, 2, 3, 4, 5, 6]
processed_numbers = list(filter(is_even, map(add_one, numbers)))
print(f"Processed numbers (map/filter): {processed_numbers}") # [4, 6, 8]