kups.core.utils.functools
¶
Functional programming utilities for composing and transforming functions.
compose(fst, snd)
¶
Chain two functions together, applying them right-to-left.
Returns a new function that applies snd first, then fst to the result.
Example
Source code in src/kups/core/utils/functools.py
constant(value)
¶
Create a function that always returns the same value, ignoring its input.
Useful for providing default values or placeholder functions.
Example
Source code in src/kups/core/utils/functools.py
curry(f)
¶
Convert a two-argument function into a chain of one-argument functions.
Useful for partial application - fixing some arguments now, others later.
Example
Source code in src/kups/core/utils/functools.py
flip(f)
¶
Swap the order of a function's first two arguments.
Useful when you have a function but need arguments in the opposite order.
Example
Source code in src/kups/core/utils/functools.py
identity(x)
¶
Return the input unchanged.
Useful as a default function or placeholder in generic code.
Source code in src/kups/core/utils/functools.py
pack_args(f)
¶
Convert a function that takes multiple arguments to one that takes a tuple.
Useful when you have data in tuples but functions that expect unpacked arguments.
Example
Source code in src/kups/core/utils/functools.py
pipe(f, g)
¶
Chain two functions together, applying them left-to-right.
Returns a new function that applies f first, then g to the result.
Example
Source code in src/kups/core/utils/functools.py
select_nth(n)
¶
Return a function that picks the n-th positional argument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Zero-based index of the argument to select. |
required |
Returns:
| Type | Description |
|---|---|
Callable[..., Any]
|
A callable |
Source code in src/kups/core/utils/functools.py
typed(f, *types)
¶
Add runtime type checking to a function.
Wraps a function to validate argument types at runtime, raising TypeError if types don't match.
Example
Source code in src/kups/core/utils/functools.py
uncurry(f)
¶
Convert a curried function back to a two-argument function.
Inverse of curry - takes nested single-argument functions and makes them accept both arguments at once.
Example
Source code in src/kups/core/utils/functools.py
unpack_args(f)
¶
Convert a function that takes a tuple to one that takes multiple arguments.
Inverse of pack_args.