bagofstuff.pipe
Provides a tool for creating a 'pipe' of functions.
Pipe
A class that provides a simple function pipeline.
Pipe is a little like Python's own partial
except that:
- It only allows for one positional parameter.
- It allows for a whole chain of functions.
Example
from bagoftools.pipe import Pipe
shortest_len = Pipe[str, int](str.trim, len)
print(shortest_len(" this is padded "))
As well as passing a list of functions when creating the pipe, they can also be added afterwards:
from bagoftools.pipe import Pipe
shortest_len = Pipe[str, int]()
shortest_len |= str.trim
shortest_len |= len
print(shortest_len(" this is padded "))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Callable[[Any], Any]
|
The initial set of functions. |
()
|
__call__
__call__(initial: TInitial) -> TResult
Execute the pipeline.
Given an initial value, it is passed to the first function in the pipeline, the result is then passed as the argument to the next function in the pipeline, and so on. The result is the result of the call to the last function in the pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
TInitial
|
The initial value. |
required |
Returns:
| Type | Description |
|---|---|
TResult
|
The result of the pipeline. |