Functools Module¶
Functional programming utilities.
This module provides utility functions for functional programming patterns, including function composition, folding, currying, and more.
The F class serves as a namespace for these utilities, providing
static methods that can be used throughout your functional code.
F Class¶
- class katharos.functools.F[source]¶
Bases:
objectNamespace for utility functions.
All functions are static and can be called without instantiating the class.
- static foldr(f, acc, xs)[source]¶
Right fold a function over an iterable.
- Parameters:
- Return type:
TypeVar(B)- Returns:
The accumulator value after applying f to each element of xs.
- static foldl(f, acc, xs)[source]¶
Left fold a function over an iterable.
- Parameters:
- Return type:
TypeVar(B)- Returns:
The accumulator value after applying f to each element of xs.
- static sigma(xs)[source]¶
Combine all elements of a non-empty list using the semigroup operation.
- Parameters:
xs (
NonEmptyList[TypeVar(A, bound=Semigroup)]) – A non-empty list of semigroup elements.- Return type:
- Returns:
The result of combining all elements using the semigroup’s @ operator.
- static curry(f)[source]¶
Transform a multi-argument function into a curried version.
Currying converts a function that takes multiple arguments into a sequence of functions, each taking a single argument. Supports both positional and keyword arguments.
- Parameters:
f (
Callable) – A function to curry.- Return type:
- Returns:
A curried version of the function.
Examples
>>> def add(x: int, y: int, z: int) -> int: ... return x + y + z >>> curried_add = F.curry(add) >>> curried_add(1)(2)(3) 6 >>> add_one = curried_add(1) >>> add_one(2)(3) 6 >>> curried_add(x=1)(y=2)(z=3) 6 >>> curried_add(1, y=2)(z=3) 6