trcks.fp.composition
Types and higher order functions for function composition.
Example
Sequentially apply two functions to one input value in three different ways:
>>> def to_length_string(n: int) -> str:
... return f"Length: {n}"
...
>>> input_ = "Hello, world!"
>>> to_length_string(len(input_))
'Length: 13'
>>> get_length_string = compose((len, to_length_string))
>>> get_length_string(input_)
'Length: 13'
>>> pipe((input_, len, to_length_string))
'Length: 13'
Composable1 = tuple[Callable[[_T0], _T1],]
module-attribute
A single function.
Composable2 = tuple[Callable[[_T0], _T1], Callable[[_T1], _T2]]
module-attribute
Two compatible functions that can be applied sequentially from first to last.
Composable3 = tuple[Callable[[_T0], _T1], Callable[[_T1], _T2], Callable[[_T2], _T3]]
module-attribute
Three compatible functions that can be applied sequentially from first to last.
Composable4 = tuple[Callable[[_T0], _T1], Callable[[_T1], _T2], Callable[[_T2], _T3], Callable[[_T3], _T4]]
module-attribute
Four compatible functions that can be applied sequentially from first to last.
Composable5 = tuple[Callable[[_T0], _T1], Callable[[_T1], _T2], Callable[[_T2], _T3], Callable[[_T3], _T4], Callable[[_T4], _T5]]
module-attribute
Five compatible functions that can be applied sequentially from first to last.
Composable6 = tuple[Callable[[_T0], _T1], Callable[[_T1], _T2], Callable[[_T2], _T3], Callable[[_T3], _T4], Callable[[_T4], _T5], Callable[[_T5], _T6]]
module-attribute
Six compatible functions that can be applied sequentially from first to last.
Composable7 = tuple[Callable[[_T0], _T1], Callable[[_T1], _T2], Callable[[_T2], _T3], Callable[[_T3], _T4], Callable[[_T4], _T5], Callable[[_T5], _T6], Callable[[_T6], _T7]]
module-attribute
Seven compatible functions that can be applied sequentially from first to last.
Composable = Union[Composable7[_IN, _T1, _T2, _T3, _T4, _T5, _T6, _OUT], Composable6[_IN, _T1, _T2, _T3, _T4, _T5, _OUT], Composable5[_IN, _T1, _T2, _T3, _T4, _OUT], Composable4[_IN, _T1, _T2, _T3, _OUT], Composable3[_IN, _T1, _T2, _OUT], Composable2[_IN, _T1, _OUT], Composable1[_IN, _OUT]]
module-attribute
Up to seven compatible functions that can be applied sequentially from first to last.
Pipeline0 = tuple[_T0,]
module-attribute
A single value.
Pipeline1 = tuple[_T0, Callable[[_T0], _T1]]
module-attribute
A single value followed by a single compatible function that can be applied.
Pipeline2 = tuple[_T0, Callable[[_T0], _T1], Callable[[_T1], _T2]]
module-attribute
A single value followed by two compatible functions that can be applied sequentially from first to last.
Pipeline3 = tuple[_T0, Callable[[_T0], _T1], Callable[[_T1], _T2], Callable[[_T2], _T3]]
module-attribute
A single value followed by three compatible functions that can be applied sequentially from first to last.
Pipeline4 = tuple[_T0, Callable[[_T0], _T1], Callable[[_T1], _T2], Callable[[_T2], _T3], Callable[[_T3], _T4]]
module-attribute
A single value followed by four compatible functions that can be applied sequentially from first to last.
Pipeline5 = tuple[_T0, Callable[[_T0], _T1], Callable[[_T1], _T2], Callable[[_T2], _T3], Callable[[_T3], _T4], Callable[[_T4], _T5]]
module-attribute
A single value followed by five compatible functions that can be applied sequentially from first to last.
Pipeline6 = tuple[_T0, Callable[[_T0], _T1], Callable[[_T1], _T2], Callable[[_T2], _T3], Callable[[_T3], _T4], Callable[[_T4], _T5], Callable[[_T5], _T6]]
module-attribute
A single value followed by six compatible functions that can be applied sequentially from first to last.
Pipeline7 = tuple[_T0, Callable[[_T0], _T1], Callable[[_T1], _T2], Callable[[_T2], _T3], Callable[[_T3], _T4], Callable[[_T4], _T5], Callable[[_T5], _T6], Callable[[_T6], _T7]]
module-attribute
A single value followed by seven compatible functions that can be applied sequentially from first to last.
Pipeline = Union[Pipeline7[_T0, _T1, _T2, _T3, _T4, _T5, _T6, _OUT], Pipeline6[_T0, _T1, _T2, _T3, _T4, _T5, _OUT], Pipeline5[_T0, _T1, _T2, _T3, _T4, _OUT], Pipeline4[_T0, _T1, _T2, _T3, _OUT], Pipeline3[_T0, _T1, _T2, _OUT], Pipeline2[_T0, _T1, _OUT], Pipeline1[_T0, _OUT], Pipeline0[_OUT]]
module-attribute
A single value followed by up to seven compatible functions that can be applied sequentially from first to last.
compose(c)
Compose a tuple of compatible functions from first to last.
Parameters:
-
c
(Composable[_IN, _T1, _T2, _T3, _T4, _T5, _T6, _OUT]
) –Compatible functions that can be applied sequentially from first to last.
Returns:
-
Callable[[_IN], _OUT]
–Function that applies the given functions from first to last.
Example
>>> get_length_string = compose((len, lambda n: f"Length: {n}"))
>>> get_length_string("Hello, world!")
'Length: 13'
Source code in src/trcks/fp/composition.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
|
pipe(p)
Evaluate a Pipeline
.
Parameters:
-
p
(Pipeline[_T0, _T1, _T2, _T3, _T4, _T5, _T6, _OUT]
) –Single value followed by up to seven compatible functions that can be applied sequentially from first to last.
Returns:
-
_OUT
–Result of sequentially applying the given functions from first to last to the given value.
Example
>>> pipe(("Hello, world!", len, lambda n: f"Length: {n}"))
'Length: 13'
Source code in src/trcks/fp/composition.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
|