Skip to content

trcks.fp.composition

Types and higher order functions for function composition.

Example

Sequentially apply two functions to one input value in three different ways:

>>> def to_length_string(n: int) -> str:
...     return f"Length: {n}"
...
>>> input_ = "Hello, world!"
>>> to_length_string(len(input_))
'Length: 13'
>>> get_length_string = compose((len, to_length_string))
>>> get_length_string(input_)
'Length: 13'
>>> pipe((input_, len, to_length_string))
'Length: 13'

Composable1 = tuple[Callable[[_T0], _T1],] module-attribute

A single function.

Composable2 = tuple[Callable[[_T0], _T1], Callable[[_T1], _T2]] module-attribute

Two compatible functions that can be applied sequentially from first to last.

Composable3 = tuple[Callable[[_T0], _T1], Callable[[_T1], _T2], Callable[[_T2], _T3]] module-attribute

Three compatible functions that can be applied sequentially from first to last.

Composable4 = tuple[Callable[[_T0], _T1], Callable[[_T1], _T2], Callable[[_T2], _T3], Callable[[_T3], _T4]] module-attribute

Four compatible functions that can be applied sequentially from first to last.

Composable5 = tuple[Callable[[_T0], _T1], Callable[[_T1], _T2], Callable[[_T2], _T3], Callable[[_T3], _T4], Callable[[_T4], _T5]] module-attribute

Five compatible functions that can be applied sequentially from first to last.

Composable6 = tuple[Callable[[_T0], _T1], Callable[[_T1], _T2], Callable[[_T2], _T3], Callable[[_T3], _T4], Callable[[_T4], _T5], Callable[[_T5], _T6]] module-attribute

Six compatible functions that can be applied sequentially from first to last.

Composable7 = tuple[Callable[[_T0], _T1], Callable[[_T1], _T2], Callable[[_T2], _T3], Callable[[_T3], _T4], Callable[[_T4], _T5], Callable[[_T5], _T6], Callable[[_T6], _T7]] module-attribute

Seven compatible functions that can be applied sequentially from first to last.

Composable = Union[Composable7[_IN, _T1, _T2, _T3, _T4, _T5, _T6, _OUT], Composable6[_IN, _T1, _T2, _T3, _T4, _T5, _OUT], Composable5[_IN, _T1, _T2, _T3, _T4, _OUT], Composable4[_IN, _T1, _T2, _T3, _OUT], Composable3[_IN, _T1, _T2, _OUT], Composable2[_IN, _T1, _OUT], Composable1[_IN, _OUT]] module-attribute

Up to seven compatible functions that can be applied sequentially from first to last.

Pipeline0 = tuple[_T0,] module-attribute

A single value.

Pipeline1 = tuple[_T0, Callable[[_T0], _T1]] module-attribute

A single value followed by a single compatible function that can be applied.

Pipeline2 = tuple[_T0, Callable[[_T0], _T1], Callable[[_T1], _T2]] module-attribute

A single value followed by two compatible functions that can be applied sequentially from first to last.

Pipeline3 = tuple[_T0, Callable[[_T0], _T1], Callable[[_T1], _T2], Callable[[_T2], _T3]] module-attribute

A single value followed by three compatible functions that can be applied sequentially from first to last.

Pipeline4 = tuple[_T0, Callable[[_T0], _T1], Callable[[_T1], _T2], Callable[[_T2], _T3], Callable[[_T3], _T4]] module-attribute

A single value followed by four compatible functions that can be applied sequentially from first to last.

Pipeline5 = tuple[_T0, Callable[[_T0], _T1], Callable[[_T1], _T2], Callable[[_T2], _T3], Callable[[_T3], _T4], Callable[[_T4], _T5]] module-attribute

A single value followed by five compatible functions that can be applied sequentially from first to last.

Pipeline6 = tuple[_T0, Callable[[_T0], _T1], Callable[[_T1], _T2], Callable[[_T2], _T3], Callable[[_T3], _T4], Callable[[_T4], _T5], Callable[[_T5], _T6]] module-attribute

A single value followed by six compatible functions that can be applied sequentially from first to last.

Pipeline7 = tuple[_T0, Callable[[_T0], _T1], Callable[[_T1], _T2], Callable[[_T2], _T3], Callable[[_T3], _T4], Callable[[_T4], _T5], Callable[[_T5], _T6], Callable[[_T6], _T7]] module-attribute

A single value followed by seven compatible functions that can be applied sequentially from first to last.

Pipeline = Union[Pipeline7[_T0, _T1, _T2, _T3, _T4, _T5, _T6, _OUT], Pipeline6[_T0, _T1, _T2, _T3, _T4, _T5, _OUT], Pipeline5[_T0, _T1, _T2, _T3, _T4, _OUT], Pipeline4[_T0, _T1, _T2, _T3, _OUT], Pipeline3[_T0, _T1, _T2, _OUT], Pipeline2[_T0, _T1, _OUT], Pipeline1[_T0, _OUT], Pipeline0[_OUT]] module-attribute

A single value followed by up to seven compatible functions that can be applied sequentially from first to last.

compose(c)

Compose a tuple of compatible functions from first to last.

Parameters:

  • c (Composable[_IN, _T1, _T2, _T3, _T4, _T5, _T6, _OUT]) –

    Compatible functions that can be applied sequentially from first to last.

Returns:

  • Callable[[_IN], _OUT]

    Function that applies the given functions from first to last.

Example
>>> get_length_string = compose((len, lambda n: f"Length: {n}"))
>>> get_length_string("Hello, world!")
'Length: 13'
Source code in src/trcks/fp/composition.py
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
def compose(  # noqa: PLR0911
    c: Composable[_IN, _T1, _T2, _T3, _T4, _T5, _T6, _OUT],
) -> Callable[[_IN], _OUT]:
    """Compose a tuple of compatible functions from first to last.

    Args:
        c: Compatible functions that can be applied sequentially from first to last.

    Returns:
        Function that applies the given functions from first to last.

    Example:
        >>> get_length_string = compose((len, lambda n: f"Length: {n}"))
        >>> get_length_string("Hello, world!")
        'Length: 13'
    """
    if len(c) == 1:
        return lambda in_: c[0](in_)
    if len(c) == 2:  # noqa: PLR2004
        return lambda in_: c[1](c[0](in_))
    if len(c) == 3:  # noqa: PLR2004
        return lambda in_: c[2](c[1](c[0](in_)))
    if len(c) == 4:  # noqa: PLR2004
        return lambda in_: c[3](c[2](c[1](c[0](in_))))
    if len(c) == 5:  # noqa: PLR2004
        return lambda in_: c[4](c[3](c[2](c[1](c[0](in_)))))
    if len(c) == 6:  # noqa: PLR2004
        return lambda in_: c[5](c[4](c[3](c[2](c[1](c[0](in_))))))
    if len(c) == 7:  # noqa: PLR2004
        return lambda in_: c[6](c[5](c[4](c[3](c[2](c[1](c[0](in_)))))))
    return assert_never(c)  # type: ignore [unreachable]  # pragma: no cover

pipe(p)

Evaluate a Pipeline.

Parameters:

  • p (Pipeline[_T0, _T1, _T2, _T3, _T4, _T5, _T6, _OUT]) –

    Single value followed by up to seven compatible functions that can be applied sequentially from first to last.

Returns:

  • _OUT

    Result of sequentially applying the given functions from first to last to the given value.

Example
>>> pipe(("Hello, world!", len, lambda n: f"Length: {n}"))
'Length: 13'
Source code in src/trcks/fp/composition.py
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
def pipe(p: Pipeline[_T0, _T1, _T2, _T3, _T4, _T5, _T6, _OUT]) -> _OUT:
    """Evaluate a `Pipeline`.

    Args:
        p:
            Single value followed by up to seven compatible functions
            that can be applied sequentially from first to last.

    Returns:
        Result of sequentially applying the given functions from first to last
            to the given value.

    Example:
        >>> pipe(("Hello, world!", len, lambda n: f"Length: {n}"))
        'Length: 13'
    """
    if len(p) == 1:
        return p[0]
    return compose(p[1:])(p[0])