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 = 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 = 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.

compose1(c)

Compose a single function.

Parameters:

Returns:

  • Callable[[_T0], _T1]

    Function that applies the given function.

Example
>>> get_length = compose1((len,))
>>> get_length("Hello, world!")
13
Source code in src/trcks/fp/composition.py
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
def compose1(c: Composable1[_T0, _T1]) -> Callable[[_T0], _T1]:
    """Compose a single function.

    Args:
        c: A single function.

    Returns:
        Function that applies the given function.

    Example:
        >>> get_length = compose1((len,))
        >>> get_length("Hello, world!")
        13
    """
    return lambda t0: c[0](t0)  # noqa: PLW0108 # for uniformity with other compose* functions

compose2(c)

Compose two compatible functions from first to last.

Parameters:

  • c (Composable2[_T0, _T1, _T2]) –

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

Returns:

  • Callable[[_T0], _T2]

    Function that applies the given functions from first to last.

Example
>>> get_length_string = compose2((len, lambda n: f"Length: {n}"))
>>> get_length_string("Hello, world!")
'Length: 13'
Source code in src/trcks/fp/composition.py
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
def compose2(c: Composable2[_T0, _T1, _T2]) -> Callable[[_T0], _T2]:
    """Compose two compatible functions from first to last.

    Args:
        c: Two 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 = compose2((len, lambda n: f"Length: {n}"))
        >>> get_length_string("Hello, world!")
        'Length: 13'
    """
    return lambda t0: c[1](c[0](t0))

compose3(c)

Compose three compatible functions from first to last.

Parameters:

  • c (Composable3[_T0, _T1, _T2, _T3]) –

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

Returns:

  • Callable[[_T0], _T3]

    Function that applies the given functions from first to last.

Example
>>> add_one = lambda n: n + 1
>>> square = lambda n: n * n
>>> to_string = lambda n: f"Result: {n}"
>>> compute = compose3((add_one, square, to_string))
>>> compute(3)
'Result: 16'
Source code in src/trcks/fp/composition.py
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
def compose3(c: Composable3[_T0, _T1, _T2, _T3]) -> Callable[[_T0], _T3]:
    """Compose three compatible functions from first to last.

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

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

    Example:
        >>> add_one = lambda n: n + 1
        >>> square = lambda n: n * n
        >>> to_string = lambda n: f"Result: {n}"
        >>> compute = compose3((add_one, square, to_string))
        >>> compute(3)
        'Result: 16'
    """
    return lambda t0: c[2](c[1](c[0](t0)))

compose4(c)

Compose four compatible functions from first to last.

Parameters:

  • c (Composable4[_T0, _T1, _T2, _T3, _T4]) –

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

Returns:

  • Callable[[_T0], _T4]

    Function that applies the given functions from first to last.

Example
>>> add_one = lambda n: n + 1
>>> square = lambda n: n * n
>>> halve = lambda n: n / 2
>>> to_string = lambda n: f"Result: {n}"
>>> compute = compose4((add_one, square, halve, to_string))
>>> compute(3)
'Result: 8.0'
Source code in src/trcks/fp/composition.py
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
def compose4(c: Composable4[_T0, _T1, _T2, _T3, _T4]) -> Callable[[_T0], _T4]:
    """Compose four compatible functions from first to last.

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

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

    Example:
        >>> add_one = lambda n: n + 1
        >>> square = lambda n: n * n
        >>> halve = lambda n: n / 2
        >>> to_string = lambda n: f"Result: {n}"
        >>> compute = compose4((add_one, square, halve, to_string))
        >>> compute(3)
        'Result: 8.0'
    """
    return lambda t0: c[3](c[2](c[1](c[0](t0))))

compose5(c)

Compose five compatible functions from first to last.

Parameters:

  • c (Composable5[_T0, _T1, _T2, _T3, _T4, _T5]) –

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

Returns:

  • Callable[[_T0], _T5]

    Function that applies the given functions from first to last.

Example
>>> add_one = lambda n: n + 1
>>> square = lambda n: n * n
>>> halve = lambda n: n / 2
>>> to_string = lambda n: f"Result: {n}"
>>> exclaim = lambda s: s + "!"
>>> compute = compose5((add_one, square, halve, to_string, exclaim))
>>> compute(3)
'Result: 8.0!'
Source code in src/trcks/fp/composition.py
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
def compose5(c: Composable5[_T0, _T1, _T2, _T3, _T4, _T5]) -> Callable[[_T0], _T5]:
    """Compose five compatible functions from first to last.

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

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

    Example:
        >>> add_one = lambda n: n + 1
        >>> square = lambda n: n * n
        >>> halve = lambda n: n / 2
        >>> to_string = lambda n: f"Result: {n}"
        >>> exclaim = lambda s: s + "!"
        >>> compute = compose5((add_one, square, halve, to_string, exclaim))
        >>> compute(3)
        'Result: 8.0!'
    """
    return lambda t0: c[4](c[3](c[2](c[1](c[0](t0)))))

compose6(c)

Compose six compatible functions from first to last.

Parameters:

  • c (Composable6[_T0, _T1, _T2, _T3, _T4, _T5, _T6]) –

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

Returns:

  • Callable[[_T0], _T6]

    Function that applies the given functions from first to last.

Example
>>> add_one = lambda n: n + 1
>>> square = lambda n: n * n
>>> halve = lambda n: n / 2
>>> to_string = lambda n: f"Result: {n}"
>>> exclaim = lambda s: s + "!"
>>> to_list = lambda s: [s]
>>> compute = compose6((add_one, square, halve, to_string, exclaim, to_list))
>>> compute(3)
['Result: 8.0!']
Source code in src/trcks/fp/composition.py
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
def compose6(c: Composable6[_T0, _T1, _T2, _T3, _T4, _T5, _T6]) -> Callable[[_T0], _T6]:
    """Compose six compatible functions from first to last.

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

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

    Example:
        >>> add_one = lambda n: n + 1
        >>> square = lambda n: n * n
        >>> halve = lambda n: n / 2
        >>> to_string = lambda n: f"Result: {n}"
        >>> exclaim = lambda s: s + "!"
        >>> to_list = lambda s: [s]
        >>> compute = compose6((add_one, square, halve, to_string, exclaim, to_list))
        >>> compute(3)
        ['Result: 8.0!']
    """
    return lambda t0: c[5](c[4](c[3](c[2](c[1](c[0](t0))))))

compose7(c)

Compose seven compatible functions from first to last.

Parameters:

  • c (Composable7[_T0, _T1, _T2, _T3, _T4, _T5, _T6, _T7]) –

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

Returns:

  • Callable[[_T0], _T7]

    Function that applies the given functions from first to last.

Example
>>> add_one = lambda n: n + 1
>>> square = lambda n: n * n
>>> halve = lambda n: n / 2
>>> to_string = lambda n: f"Result: {n}"
>>> exclaim = lambda s: s + "!"
>>> to_list = lambda s: [s]
>>> wrap_in_dict = lambda lst: {"result": lst}
>>> compute = compose7(
...     (add_one, square, halve, to_string, exclaim, to_list, wrap_in_dict)
... )
>>> compute(3)
{'result': ['Result: 8.0!']}
Source code in src/trcks/fp/composition.py
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
def compose7(
    c: Composable7[_T0, _T1, _T2, _T3, _T4, _T5, _T6, _T7],
) -> Callable[[_T0], _T7]:
    """Compose seven compatible functions from first to last.

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

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

    Example:
        >>> add_one = lambda n: n + 1
        >>> square = lambda n: n * n
        >>> halve = lambda n: n / 2
        >>> to_string = lambda n: f"Result: {n}"
        >>> exclaim = lambda s: s + "!"
        >>> to_list = lambda s: [s]
        >>> wrap_in_dict = lambda lst: {"result": lst}
        >>> compute = compose7(
        ...     (add_one, square, halve, to_string, exclaim, to_list, wrap_in_dict)
        ... )
        >>> compute(3)
        {'result': ['Result: 8.0!']}
    """
    return lambda t0: c[6](c[5](c[4](c[3](c[2](c[1](c[0](t0)))))))

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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
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'
    """
    match c:
        case (_,):
            return compose1(c)
        case (_, _):
            return compose2(c)
        case (_, _, _):
            return compose3(c)
        case (_, _, _, _):
            return compose4(c)
        case (_, _, _, _, _):
            return compose5(c)
        case (_, _, _, _, _, _):
            return compose6(c)
        case (_, _, _, _, _, _, _):
            return compose7(c)
        case _:  # pragma: no cover
            assert_type(c, Never)  # type: ignore[unreachable]  # pyright: ignore[reportUnreachable]
            msg = f"{type(c).__name__!r} is not a valid Composable"
            raise TypeError(msg)

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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
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'
    """
    match p:
        case (value,):
            return value
        case (value, _, *_):
            composable = p[1:]
            return compose(composable)(value)
        case _:  # pragma: no cover
            assert_type(p, Never)  # type: ignore[unreachable]  # pyright: ignore[reportUnreachable]  # pyrefly: ignore [assert-type]
            msg = f"{type(p).__name__!r} is not a valid Pipeline"
            raise TypeError(msg)