Skip to content

Composition

pipe_once(inner: Unary[T, U], outer: Unary[U, R]) -> Unary[T, R]

Composes two functions from left to right into one function.

For instance, pipe_once(f, g)(x) is equivalent to g(f(x)).

Parameters:

Name Type Description Default
inner Unary[T, U]

The inner function.

required
outer Unary[U, R]

The outer function.

required

Returns:

Type Description
Unary[T, R]

The composed function.

Source code in src/funcs/composition.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def pipe_once(inner: Unary[T, U], outer: Unary[U, R]) -> Unary[T, R]:
    """Composes two functions from left to right into one function.

    For instance, `pipe_once(f, g)(x)` is equivalent to `g(f(x))`.

    Arguments:
        inner: The inner function.
        outer: The outer function.

    Returns:
        The composed function.
    """

    def piped(item: T) -> R:
        return outer(inner(item))

    return piped

pipe(innermost: Unary[T, Any], *functions: Unary[Any, Any]) -> Unary[T, Any]

Composes multiple functions from left to right into one function.

For instance, pipe(f, g)(x) is equivalent to g(f(x)).

This function is equivalent to:

fold(innermost, pipe_once, functions)

Parameters:

Name Type Description Default
innermost Unary[T, Any]

The innermost function.

required
*functions Unary[Any, Any]

The rest of the functions.

()

Returns:

Type Description
Unary[T, Any]

The composed function.

Source code in src/funcs/composition.py
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
def pipe(innermost: Unary[T, Any], *functions: Unary[Any, Any]) -> Unary[T, Any]:
    """Composes multiple functions from left to right into one function.

    For instance, `pipe(f, g)(x)` is equivalent to `g(f(x))`.

    This function is equivalent to:

    ```python
    fold(innermost, pipe_once, functions)
    ```

    Arguments:
        innermost: The innermost function.
        *functions: The rest of the functions.

    Returns:
        The composed function.
    """
    return fold(innermost, pipe_once, functions)

compose_once(outer: Unary[U, R], inner: Unary[T, U]) -> Unary[T, R]

Composes two functions from right to left into one function.

For instance, compose_once(f, g)(x) is equivalent to f(g(x)).

Parameters:

Name Type Description Default
outer Unary[U, R]

The outer function.

required
inner Unary[T, U]

The inner function.

required

Returns:

Type Description
Unary[T, R]

The composed function.

Source code in src/funcs/composition.py
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
def compose_once(outer: Unary[U, R], inner: Unary[T, U]) -> Unary[T, R]:
    """Composes two functions from right to left into one function.

    For instance, `compose_once(f, g)(x)` is equivalent to `f(g(x))`.

    Arguments:
        outer: The outer function.
        inner: The inner function.

    Returns:
        The composed function.
    """

    def composed(item: T) -> R:
        return outer(inner(item))

    return composed

compose(outermost: Unary[Any, T], *functions: Unary[Any, Any]) -> Unary[Any, T]

Composes multiple functions from right to left into one.

For instance, compose(f, g)(x) is equivalent to f(g(x)).

This function is equivalent to:

fold(outermost, compose_once, functions)

Parameters:

Name Type Description Default
outermost Unary[Any, T]

The outermost function.

required
*functions Unary[Any, Any]

The rest of the functions.

()

Returns:

Type Description
Unary[Any, T]

The composed function.

Source code in src/funcs/composition.py
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
def compose(outermost: Unary[Any, T], *functions: Unary[Any, Any]) -> Unary[Any, T]:
    """Composes multiple functions from right to left into one.

    For instance, `compose(f, g)(x)` is equivalent to `f(g(x))`.

    This function is equivalent to:

    ```python
    fold(outermost, compose_once, functions)
    ```

    Arguments:
        outermost: The outermost function.
        *functions: The rest of the functions.

    Returns:
        The composed function.
    """
    return fold(outermost, compose_once, functions)