fptools.function

Pythonic FP - FP tools for functions

Not a replacement for the std library’s functools which is more about modifying function behavior through decorators than functional composition and application.

FP utilities to manipulate and partially apply functions

  • function swap - Swap the arguments of a 2 argument function

  • function sequenced - Convert function to take a sequence of its arguments

  • function negate - Transforms a predicate to its negation

  • function partial - Returns a partially applied function

pythonic_fp.fptools.function.negate(f: Callable[[P], bool]) Callable[[P], bool]

Take a predicate and return its negation.

pythonic_fp.fptools.function.partial(f: Callable[[P], R], *args: Any) Callable[[...], R]

Partially apply arguments to a function, left to right.

  • type-wise the only thing guaranteed is the return type

  • best practice is to cast the result immediately

pythonic_fp.fptools.function.sequenced(f: Callable[[...], R]) Callable[[tuple[Any]], R]

Convert a function with arbitrary positional arguments to one taking a tuple of the original arguments.

  • was awaiting typing and mypy “improvements” to ParamSpec

    • return type: Callable[tuple[P.args], R] ???

    • return type: Callable[[tuple[P.args]], R] ???

  • not going to happen, see

TODO: Look into replacing this function with a Callable class?

pythonic_fp.fptools.function.swap(f: Callable[[U, V], R]) Callable[[V, U], R]

Swap arguments of a two argument function.