Functional Programming¶
extepy
provides APIs for argument reorganizing and execution.
Function Execution¶
Reorganize Arguments¶
extepy
provides APIs for refactoring arguments of callable functions. They can be used as decorators of functions.
- extepy.pack(f)[source]¶
Merge all positional arguments of a function to a single tuple argument
- Parameters:
f (callable)
- Return type:
callable
Examples
Apply on a function.
>>> packed = pack(isinstance) >>> packed([1.0, float]) True
Use as a decorator.
>>> import math >>> @pack ... def g(*args): ... return sum(arg + 1 for arg in args) >>> g([1, 2, 3]) 9
- extepy.unpack(f)[source]¶
Replace a single tuple/list argument to many positional arguments.
- Parameters:
f (callable)
- Return type:
callable
Examples
Apply on a function.
>>> unpacked = unpack(all) >>> unpacked(True, True, True) True
Use as decorator.
>>> @unpack ... def g(values): ... return max(values) - min(values) >>> g(1, 2, 3) 2
Function Composition¶
- extepy.repeat(n=1)[source]¶
Repeat a callable n times.
- Parameters:
n (int) – Number of times to repeat.
- Returns:
a callable that swaps a callable so that it is called n times.
- Return type:
Callable[callable, callable]
Examples
Apply on a function.
>>> repeated = repeat(n=2)(abs) >>> repeated(-3) 3
Use as a decorator.
>>> @repeat(n=2) ... def g(a): ... return a + 1 >>> g(1) 3
- extepy.skewer(*callables)[source]¶
Composite multiple callables into one callable.
- Parameters:
*callables (callable)
- Returns:
A callable that calls all callables in order.
- Return type:
callable
Examples
>>> def minmax(x, y): ... return min(x, y), max(x, y) >>> def mul(x, y): ... return x * y >>> skewered = skewer(minmax, mul) >>> skewered(5, 3) 15