cytoolz Documentation

repository·master·Indexed 22 days ago

https://github.com/pytoolz/cytoolz

A high-performance Cython implementation of the toolz library providing functional utilities for iterables, functions, and dictionaries. It serves as a drop-in replacement for toolz with improved speed and memory efficiency, featuring submodules such as itertoolz, functoolz, dicttoolz, and recipes.

Tokens
1.1K
Snippets
5
Records
6
Agent score
28%

What's inside cytoolz

  1. Understand the relationship between cytoolz and toolz

    master

    cytoolz is a high-performance Cython implementation of the toolz package.

    Key characteristics:

    • API Compatibility: cytoolz implements the exact same API as toolz, making it a drop-in replacement for many use cases.
    • Performance: It is typically 2-5x faster than the pure Python toolz implementation.
    • C API: Unlike toolz, cytoolz offers a C API that can be accessed by other projects developed in Cython.
    • Memory Efficiency: Like toolz, it uses the iterator protocol and returns iterators whenever possible to handle large or infinite datasets with low memory usage.

    Note on Threading: cytoolz has experimental support for Python free-threading (introduced in Python 3.13) and provides wheels for free-threaded Python. However, it has not been explicitly developed or tested for thread-safety; use it in multi-threaded environments at your own risk.

  2. Use cytoolz for high-performance functional utilities

    master

    CyToolz provides high-performance implementations of functional utility functions for iterables, functions, and dictionaries. It is designed as a faster alternative to the toolz library. The package exports functions from several submodules:

    • itertoolz: Functions for working with iterables.
    • functoolz: Functions for working with functions.
    • dicttoolz: Functions for working with dictionaries.
    • recipes: Higher-level functional patterns and combinations.
    • curried: A sandbox module containing curried versions of functions (use with caution).

    Standard Python built-ins like map, filter, sorted, partial, and reduce are also available through the top-level namespace.

    import cytoolz
    
    # Accessing functions directly from the top-level
    # (Assuming functions are imported via the * imports in __init__.py)
    # Example usage of typical toolz-style functions:
    # cytoolz.groupby(...)
    # cytoolz.compose(...)
    # cytoolz.valmap(...)
  3. Compose functions using comp

    master

    The comp function is an alias for compose. It allows you to create a new function by composing multiple functions together, where the output of one function becomes the input of the next (right-to-left execution).

    from cytoolz import comp
    
    def add_one(x): return x + 1
    def double(x): return x * 2
    
    # f(x) = add_one(double(x))
    add_then_double = comp(add_one, double)
    
    print(add_then_double(5))  # Output: 11
  4. Use always-curried flip and memoize

    master

    In cytoolz, the following functions are explicitly curried, meaning they can be partially applied to their arguments:

    • flip: Reverses the order of arguments in a function.
    • memoize: Creates a memoized version of a function (caching results based on input arguments).

    Note: These are available at the top level and are also accessible via cytoolz.functoolz.

    from cytoolz import flip, memoize
    
    # Example: flip
    def add(a, b): return a + b
    add_five = flip(add)(5)
    print(add_five(10))  # Output: 15
    
    # Example: memoize
    @memoize
    def expensive_func(x):
        print(f"Computing {x}...")
        return x * x
    
    expensive_func(4)
    expensive_func(4)  # Uses cache, no print statement