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
Swap arguments of a two argument function.
- param f:
Two argument function.
- returns:
A version of
fwith its arguments swapped.
- pythonic_fp.fptools.function.compose(f: Callable[[D], T], g: Callable[[T], R]) Callable[[D], R]¶
compose
Function Composition
- param f:
Function called first with domain
Dand rangeT.- param g:
Function called on result with domain
Tand rangeR.- returns:
The composite function
g∘f(d) = g(f(d))
- pythonic_fp.fptools.function.negate(f: Callable[[P], bool]) Callable[[P], bool]¶
negate
Take a predicate and return its negation.
- param f:
a function
fwhich returns a bool- returns:
the function
not f
- pythonic_fp.fptools.function.sequenced(f: Callable[[...], R]) Callable[[tuple[Any]], R]¶
sequenced
Convert a function with arbitrary positional arguments to one taking a tuple of the original arguments.
- param f:
Function with just positional parameters
- returns:
An equivalent function taking a tuple of the arguments to
f.
- pythonic_fp.fptools.function.partial(f: Callable[[P], R], *args: Any) Callable[[...], R]¶
partial
Partially apply arguments to a function with positional arguments left to right.
- param f:
Function with just positional arguments.
- param args:
Arguments to partially apply to
f.
Warning
The types of the arguments are lost.
Tip
Best practice is to cast the result if it is not consumed immediately.