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: object

Namespace for utility functions.

All functions are static and can be called without instantiating the class.

static compose(f)[source]

Compose two functions.

Parameters:

f (Callable[[TypeVar(B)], TypeVar(C)]) – A function from B to C.

Return type:

Callable[[Callable[[TypeVar(A)], TypeVar(B)]], Callable[[TypeVar(A)], TypeVar(C)]]

Returns:

A function that takes a function from A to B and returns a function from A to C.

static id(x)[source]

Identity function.

Parameters:

x (TypeVar(A)) – Input value.

Return type:

TypeVar(A)

Returns:

The same value x.

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:

TypeVar(A, bound= Semigroup)

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:

Callable

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