fptools.function

FP tools for functions

FP utilities to manipulate and partially apply functions.

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

  • function compose - Function composition

  • function negate - Transforms a predicate to its negation

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

  • function partial - Returns a partially applied function

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

Swap arguments of a two argument function.

Parameters:

f – Two argument function.

Returns:

A version of f with its arguments swapped.

pythonic_fp.fptools.function.compose(f: Callable[[D], T], g: Callable[[T], R]) Callable[[D], R]

Function Composition

Parameters:
  • f – Function called first with domain D and range T.

  • g – Function called on result with domain T and range R.

Returns:

The composite function g∘f(d) = g(f(d))

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

Take a predicate and return its negation.

Parameters:

f – a function f which returns a bool

Returns:

the function not f

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

Convert a function from multi-to-single valued.

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] ???

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

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

Partial function application.

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