Module 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 it - Function returning an iterator of its arguments

  • 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.it(*args)

Function returning an iterator of its arguments.

Parameters:

args (TypeVar(A))

Return type:

Iterator[TypeVar(A)]

pythonic_fp.fptools.function.negate(f)

Take a predicate and return its negation.

Parameters:

f (Callable[[ParamSpec(P)], bool])

Return type:

Callable[[ParamSpec(P)], bool]

pythonic_fp.fptools.function.partial(f, *args)

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

Parameters:
  • f (Callable[[ParamSpec(P)], TypeVar(R)])

  • args (Any)

Return type:

Callable[..., TypeVar(R)]

pythonic_fp.fptools.function.sequenced(f)

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?

Parameters:

f (Callable[..., TypeVar(R)])

Return type:

Callable[[tuple[Any]], TypeVar(R)]

pythonic_fp.fptools.function.swap(f)

Swap arguments of a two argument function.

Parameters:

f (Callable[[TypeVar(U), TypeVar(V)], TypeVar(R)])

Return type:

Callable[[TypeVar(V), TypeVar(U)], TypeVar(R)]