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.
compose1(c)
Compose a single function.
Parameters:
-
c
(Composable1[_T0, _T1]
) –A single function.
Returns:
-
Callable[[_T0], _T1]
–Function that applies the given function.
Example
>>> get_length = compose1((len,))
>>> get_length("Hello, world!")
13
Source code in src/trcks/fp/composition.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
|
compose2(c)
Compose two compatible functions from first to last.
Parameters:
-
c
(Composable2[_T0, _T1, _T2]
) –Two compatible functions that can be applied sequentially from first to last.
Returns:
-
Callable[[_T0], _T2]
–Function that applies the given functions from first to last.
Example
>>> get_length_string = compose2((len, lambda n: f"Length: {n}"))
>>> get_length_string("Hello, world!")
'Length: 13'
Source code in src/trcks/fp/composition.py
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
|
compose3(c)
Compose three compatible functions from first to last.
Parameters:
-
c
(Composable3[_T0, _T1, _T2, _T3]
) –Three compatible functions that can be applied sequentially from first to last.
Returns:
-
Callable[[_T0], _T3]
–Function that applies the given functions from first to last.
Example
>>> add_one = lambda n: n + 1
>>> square = lambda n: n * n
>>> to_string = lambda n: f"Result: {n}"
>>> compute = compose3((add_one, square, to_string))
>>> compute(3)
'Result: 16'
Source code in src/trcks/fp/composition.py
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
|
compose4(c)
Compose four compatible functions from first to last.
Parameters:
-
c
(Composable4[_T0, _T1, _T2, _T3, _T4]
) –Four compatible functions that can be applied sequentially from first to last.
Returns:
-
Callable[[_T0], _T4]
–Function that applies the given functions from first to last.
Example
>>> add_one = lambda n: n + 1
>>> square = lambda n: n * n
>>> halve = lambda n: n / 2
>>> to_string = lambda n: f"Result: {n}"
>>> compute = compose4((add_one, square, halve, to_string))
>>> compute(3)
'Result: 8.0'
Source code in src/trcks/fp/composition.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
|
compose5(c)
Compose five compatible functions from first to last.
Parameters:
-
c
(Composable5[_T0, _T1, _T2, _T3, _T4, _T5]
) –Five compatible functions that can be applied sequentially from first to last.
Returns:
-
Callable[[_T0], _T5]
–Function that applies the given functions from first to last.
Example
>>> add_one = lambda n: n + 1
>>> square = lambda n: n * n
>>> halve = lambda n: n / 2
>>> to_string = lambda n: f"Result: {n}"
>>> exclaim = lambda s: s + "!"
>>> compute = compose5((add_one, square, halve, to_string, exclaim))
>>> compute(3)
'Result: 8.0!'
Source code in src/trcks/fp/composition.py
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
|
compose6(c)
Compose six compatible functions from first to last.
Parameters:
-
c
(Composable6[_T0, _T1, _T2, _T3, _T4, _T5, _T6]
) –Six compatible functions that can be applied sequentially from first to last.
Returns:
-
Callable[[_T0], _T6]
–Function that applies the given functions from first to last.
Example
>>> add_one = lambda n: n + 1
>>> square = lambda n: n * n
>>> halve = lambda n: n / 2
>>> to_string = lambda n: f"Result: {n}"
>>> exclaim = lambda s: s + "!"
>>> to_list = lambda s: [s]
>>> compute = compose6((add_one, square, halve, to_string, exclaim, to_list))
>>> compute(3)
['Result: 8.0!']
Source code in src/trcks/fp/composition.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
|
compose7(c)
Compose seven compatible functions from first to last.
Parameters:
-
c
(Composable7[_T0, _T1, _T2, _T3, _T4, _T5, _T6, _T7]
) –Seven compatible functions that can be applied sequentially from first to last.
Returns:
-
Callable[[_T0], _T7]
–Function that applies the given functions from first to last.
Example
>>> add_one = lambda n: n + 1
>>> square = lambda n: n * n
>>> halve = lambda n: n / 2
>>> to_string = lambda n: f"Result: {n}"
>>> exclaim = lambda s: s + "!"
>>> to_list = lambda s: [s]
>>> wrap_in_dict = lambda lst: {"result": lst}
>>> compute = compose7(
... (add_one, square, halve, to_string, exclaim, to_list, wrap_in_dict)
... )
>>> compute(3)
{'result': ['Result: 8.0!']}
Source code in src/trcks/fp/composition.py
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
|
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
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
|
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
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
|