Skip to content

trcks.fp.monads.identity

Functions for the identity monad.

Provides utilities for functional composition of synchronous functions.

tap(f)

Turn synchronous function into a function that returns its input.

Parameters:

  • f (Callable[[_T], object]) –

    The synchronous function to be transformed into a function that returns its input.

Returns:

  • Callable[[_T], _T]

    The given function transformed into a function that returns its input.

Example
>>> from collections.abc import Callable
>>> from trcks.fp.monads import identity as i
>>> log_and_pass_on: Callable[[object], object] = i.tap(
...     lambda o: print(f"Received object {o}.")
... )
>>> output = log_and_pass_on(42)
Received object 42.
>>> output
42
Source code in src/trcks/fp/monads/identity.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def tap(f: Callable[[_T], object]) -> Callable[[_T], _T]:
    """Turn synchronous function into a function that returns its input.

    Args:
        f:
            The synchronous function to be transformed into
            a function that returns its input.

    Returns:
        The given function transformed into a function that returns its input.

    Example:
        >>> from collections.abc import Callable
        >>> from trcks.fp.monads import identity as i
        >>> log_and_pass_on: Callable[[object], object] = i.tap(
        ...     lambda o: print(f"Received object {o}.")
        ... )
        >>> output = log_and_pass_on(42)
        Received object 42.
        >>> output
        42
    """

    def bypassed_f(value: _T) -> _T:
        _ = f(value)
        return value

    return bypassed_f