cytoolz Library

1.1.0 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This quickstart demonstrates basic usage of `compose`, `pipe`, `map`, and `filter` from `cytoolz.functoolz` and `cytoolz.itertoolz`. These functions provide a functional programming style with potential performance benefits over their pure Python equivalents, especially for iterable operations.

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]

view raw JSON →