trcks.fp.monads.result_tuple
Monadic functions for trcks.ResultTuple.
Provides utilities for functional composition of functions returning trcks.ResultTuple values.
Example
Map and tap each element inside a success tuple:
>>> from trcks.fp.composition import pipe
>>> from trcks.fp.monads import result_tuple as rt
>>> def double_integer(n: int) -> int:
... return n * 2
...
>>> def duplicate_integer(n: int) -> tuple[int, int]:
... return n, n
...
>>> def log_integer(n: int) -> None:
... print(f"Received: {n}")
...
>>> result_tuple = pipe(
... (
... rt.construct_successes_from_iterable((1, 2, 3)),
... rt.map_successes(double_integer),
... rt.tap_successes(log_integer),
... rt.map_successes_to_iterable(duplicate_integer),
... )
... )
Received: 2
Received: 4
Received: 6
>>> result_tuple
('success', (2, 2, 4, 4, 6, 6))
construct_failure(value)
Create a trcks.Failure object from a value.
Parameters:
-
value(_F) –Value to be wrapped in a trcks.Failure.
Returns:
-
Failure[_F]–trcks.Failure object containing the given value.
Note
This function is an alias for trcks.fp.monads.result.construct_failure.
Example
>>> from trcks.fp.monads import result_tuple as rt
>>> rt.construct_failure("not found")
('failure', 'not found')
Source code in src/trcks/fp/monads/result_tuple.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
construct_from_result(rslt)
Create a trcks.ResultTuple object from a trcks.Result.
Parameters:
-
rslt(Result[_F, _S]) –The trcks.Result object to be wrapped.
Returns:
-
ResultTuple[_F, _S]–A new trcks.ResultTuple instance with the success payload wrapped in a homogeneous tuple.
Example
>>> from trcks.fp.monads import result_tuple as rt
>>> rt.construct_from_result(("success", 7))
('success', (7,))
>>> rt.construct_from_result(("failure", "oops"))
('failure', 'oops')
Source code in src/trcks/fp/monads/result_tuple.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
construct_successes(value)
Create a trcks.SuccessTuple object from a single value.
Parameters:
-
value(_S) –A single value.
Returns:
-
SuccessTuple[_S]–A new trcks.SuccessTuple instance containing the single value.
Example
>>> from trcks.fp.monads import result_tuple as rt
>>> rt.construct_successes(42)
('success', (42,))
Source code in src/trcks/fp/monads/result_tuple.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
construct_successes_from_iterable(it)
Create a trcks.SuccessTuple object from an iterable.
Parameters:
-
it(Iterable[_S]) –The iterable to create the trcks.SuccessTuple from.
Returns:
-
SuccessTuple[_S]–The trcks.SuccessTuple created from the iterable.
Example
>>> from trcks.fp.monads import result_tuple as rt
>>> rt.construct_successes_from_iterable((1, 2))
('success', (1, 2))
Source code in src/trcks/fp/monads/result_tuple.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
construct_successes_from_tuple(tpl)
Deprecated alias for trcks.fp.monads.result_tuple.construct_successes_from_iterable.
Source code in src/trcks/fp/monads/result_tuple.py
135 136 137 138 139 140 | |
map_failure(f)
Create function that maps trcks.Failure values to trcks.Failure values.
trcks.SuccessTuple values are left unchanged.
Parameters:
-
f(Callable[[_F1], _F2]) –Function to apply to the trcks.Failure values.
Returns:
-
Callable[[ResultTuple[_F1, _S1]], ResultTuple[_F2, _S1]]–Maps trcks.Failure values to new trcks.Failure values according to the given function and leaves trcks.SuccessTuple values unchanged.
Example
>>> from collections.abc import Callable
>>> from trcks import ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _add_prefix(description: str) -> str:
... return f"err: {description}"
...
>>> add_prefix: Callable[
... [ResultTuple[str, int]], ResultTuple[str, int]
... ] = rt.map_failure(_add_prefix)
>>> add_prefix(("failure", "not found"))
('failure', 'err: not found')
>>> add_prefix(("success", (1, 2)))
('success', (1, 2))
Source code in src/trcks/fp/monads/result_tuple.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
map_failure_to_iterable(f)
Create function that maps trcks.Failure values to homogeneous tuples.
trcks.SuccessTuple values are left unchanged.
Parameters:
-
f(Callable[[_F1], Iterable[_S2]]) –Function to apply to the trcks.Failure values.
Returns:
-
Callable[[ResultTuple[_F1, _S1]], SuccessTuple[_S1] | SuccessTuple[_S2]]–Maps trcks.Failure values to homogeneous tuples wrapped in a trcks.Success according to the given function and leaves trcks.SuccessTuple values unchanged.
Example
>>> from collections.abc import Callable
>>> from trcks import ResultTuple, SuccessTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _recover_from_not_found(description: str) -> tuple[int, ...]:
... if description == "not found":
... return (0,)
... return ()
...
>>> recover_from_not_found: Callable[
... [ResultTuple[str, int]], SuccessTuple[int]
... ] = rt.map_failure_to_iterable(_recover_from_not_found)
>>> recover_from_not_found(("failure", "not found"))
('success', (0,))
>>> recover_from_not_found(("failure", "not authorized"))
('success', ())
>>> recover_from_not_found(("success", (1, 2)))
('success', (1, 2))
Source code in src/trcks/fp/monads/result_tuple.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 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 | |
map_failure_to_result(f)
Create function that maps trcks.Failure values to trcks.Failure and trcks.Success values.
trcks.SuccessTuple values are left unchanged.
Parameters:
-
f(Callable[[_F1], Result[_F2, _S2]]) –Function to apply to the trcks.Failure values.
Returns:
-
Callable[[ResultTuple[_F1, _S1]], Result[_F2, tuple[_S1, ...] | tuple[_S2, ...]]]–Maps trcks.Failure values to new trcks.Failure and trcks.Success values according to the given function and leaves trcks.SuccessTuple values unchanged.
Example
>>> from collections.abc import Callable
>>> from trcks import ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _recover_from_not_found(description: str) -> Result[str, int]:
... if description == "not found":
... return "success", 0
... return "failure", description
...
>>> recover_from_not_found: Callable[
... [ResultTuple[str, int]], ResultTuple[str, int]
... ] = rt.map_failure_to_result(_recover_from_not_found)
>>> recover_from_not_found(("failure", "not found"))
('success', (0,))
>>> recover_from_not_found(("failure", "not authorized"))
('failure', 'not authorized')
>>> recover_from_not_found(("success", (1, 2)))
('success', (1, 2))
Source code in src/trcks/fp/monads/result_tuple.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | |
map_failure_to_result_iterable(f)
Create function that maps trcks.Failure values to new trcks.ResultTuple values.
trcks.SuccessTuple values are left unchanged.
Parameters:
-
f(Callable[[_F1], ResultIterable[_F2, _S2]]) –Function to apply to the trcks.Failure values.
Returns:
-
Callable[[ResultTuple[_F1, _S1]], Result[_F2, tuple[_S1, ...] | tuple[_S2, ...]]]–Maps trcks.Failure values to new trcks.ResultTuple values according to the given function and leaves trcks.SuccessTuple values unchanged.
Example
>>> from collections.abc import Callable
>>> from trcks import ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _recover_from_not_found(description: str) -> ResultTuple[str, int]:
... if description == "not found":
... return "success", (0,)
... return "failure", description
...
>>> recover_from_not_found: Callable[
... [ResultTuple[str, int]], ResultTuple[str, int]
... ] = rt.map_failure_to_result_iterable(_recover_from_not_found)
>>> recover_from_not_found(("failure", "not found"))
('success', (0,))
>>> recover_from_not_found(("failure", "not authorized"))
('failure', 'not authorized')
>>> recover_from_not_found(("success", (1, 2)))
('success', (1, 2))
Source code in src/trcks/fp/monads/result_tuple.py
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | |
map_failure_to_result_tuple(f)
Deprecated alias for trcks.fp.monads.result_tuple.map_failure_to_result_iterable.
Source code in src/trcks/fp/monads/result_tuple.py
304 305 306 307 308 309 310 311 | |
map_failure_to_tuple(f)
Deprecated alias for trcks.fp.monads.result_tuple.map_failure_to_iterable.
Source code in src/trcks/fp/monads/result_tuple.py
314 315 316 317 318 319 | |
map_successes(f)
Create function that maps each element of a trcks.SuccessTuple to a new element.
trcks.Failure values are left unchanged.
Parameters:
-
f(Callable[[_S1], _S2]) –Function to apply to each element of the trcks.SuccessTuple.
Returns:
-
Callable[[ResultTuple[_F1, _S1]], ResultTuple[_F1, _S2]]–Leaves trcks.Failure values unchanged and maps each element of a trcks.SuccessTuple to a new element according to the given function.
Example
>>> from collections.abc import Callable
>>> from trcks import ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _double_integer(n: int) -> int:
... return n * 2
...
>>> double_integers: Callable[
... [ResultTuple[str, int]], ResultTuple[str, int]
... ] = rt.map_successes(_double_integer)
>>> double_integers(("success", (1, 2, 3)))
('success', (2, 4, 6))
>>> double_integers(("failure", "not found"))
('failure', 'not found')
Source code in src/trcks/fp/monads/result_tuple.py
322 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 351 352 353 | |
map_successes_to_iterable(f)
Create function that maps each element of a trcks.SuccessTuple to a collections.abc.Iterable.
trcks.Failure values are left unchanged.
Parameters:
-
f(Callable[[_S1], Iterable[_S2]]) –Function to apply to each element of the trcks.SuccessTuple.
Returns:
-
Callable[[ResultTuple[_F1, _S1]], ResultTuple[_F1, _S2]]–Leaves trcks.Failure values unchanged and flat-maps each element of a trcks.SuccessTuple to a collections.abc.Iterable according to the given function.
Example
>>> from collections.abc import Callable
>>> from trcks import ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _duplicate_integer(n: int) -> tuple[int, int]:
... return n, n
...
>>> duplicate_integers: Callable[
... [ResultTuple[str, int]], ResultTuple[str, int]
... ] = rt.map_successes_to_iterable(_duplicate_integer)
>>> duplicate_integers(("success", (1, 2)))
('success', (1, 1, 2, 2))
>>> duplicate_integers(("failure", "not found"))
('failure', 'not found')
Source code in src/trcks/fp/monads/result_tuple.py
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 384 385 386 387 | |
map_successes_to_result(f)
Create function that maps each element of a trcks.SuccessTuple to trcks.Failure and trcks.Success values.
trcks.Failure values are left unchanged.
Parameters:
-
f(Callable[[_S1], Result[_F2, _S2]]) –Function to apply to each element of the trcks.SuccessTuple.
Returns:
-
Callable[[ResultTuple[_F1, _S1]], ResultTuple[_F1 | _F2, _S2]]–Leaves trcks.Failure values unchanged and maps each element of a trcks.SuccessTuple to trcks.Failure and trcks.Success values according to the given function, returning the first trcks.Failure encountered, if any.
Example
>>> from collections.abc import Callable
>>> from trcks import Result, ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _double_if_positive(n: int) -> Result[str, int]:
... if n > 0:
... return "success", n * 2
... return "failure", "not positive"
...
>>> double_if_positive: Callable[
... [ResultTuple[str, int]], ResultTuple[str, int]
... ] = rt.map_successes_to_result(_double_if_positive)
>>> double_if_positive(("success", (1, 2)))
('success', (2, 4))
>>> double_if_positive(("success", (1, -1, 2)))
('failure', 'not positive')
>>> double_if_positive(("failure", "oops"))
('failure', 'oops')
Source code in src/trcks/fp/monads/result_tuple.py
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 | |
map_successes_to_result_iterable(f)
Create function that maps each element of a trcks.SuccessTuple to new trcks.ResultTuple values.
trcks.Failure values are left unchanged.
Parameters:
-
f(Callable[[_S1], ResultIterable[_F2, _S2]]) –Function to apply to each element of the trcks.SuccessTuple.
Returns:
-
Callable[[ResultTuple[_F1, _S1]], ResultTuple[_F1 | _F2, _S2]]–Leaves trcks.Failure values unchanged and maps each element of a trcks.SuccessTuple to new trcks.ResultTuple values according to the given function, returning the first trcks.Failure returned by
f, if any.
Example
>>> from collections.abc import Callable
>>> from trcks import ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _duplicate_if_positive(n: int) -> ResultTuple[str, int]:
... if n > 0:
... return "success", (n, n)
... return "failure", "not positive"
...
>>> duplicate_if_positive: Callable[
... [ResultTuple[str, int]], ResultTuple[str, int]
... ] = rt.map_successes_to_result_iterable(_duplicate_if_positive)
>>> duplicate_if_positive(("success", (1, 2)))
('success', (1, 1, 2, 2))
>>> duplicate_if_positive(("success", (1, -1, 2)))
('failure', 'not positive')
>>> duplicate_if_positive(("failure", "oops"))
('failure', 'oops')
Source code in src/trcks/fp/monads/result_tuple.py
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 | |
map_successes_to_result_tuple(f)
Deprecated alias for trcks.fp.monads.result_tuple.map_successes_to_result_iterable.
Source code in src/trcks/fp/monads/result_tuple.py
494 495 496 497 498 499 500 501 | |
map_successes_to_tuple(f)
Deprecated alias for trcks.fp.monads.result_tuple.map_successes_to_iterable.
Source code in src/trcks/fp/monads/result_tuple.py
504 505 506 507 508 509 510 511 | |
tap_failure(f)
Create function that applies a side effect to trcks.Failure values.
trcks.SuccessTuple values are passed on without side effects.
Parameters:
-
f(Callable[[_F1], object]) –Side effect to apply to the trcks.Failure value.
Returns:
-
Callable[[ResultTuple[_F1, _S1]], ResultTuple[_F1, _S1]]–Applies the given side effect to trcks.Failure values and returns the original trcks.Failure value. Passes on trcks.SuccessTuple values without side effects.
Example
>>> from collections.abc import Callable
>>> from trcks import ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _log_error(description: str) -> None:
... print(f"Error: {description}")
...
>>> log_error: Callable[
... [ResultTuple[str, int]], ResultTuple[str, int]
... ] = rt.tap_failure(_log_error)
>>> log_error(("failure", "oops"))
Error: oops
('failure', 'oops')
>>> log_error(("success", (1,)))
('success', (1,))
Source code in src/trcks/fp/monads/result_tuple.py
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 | |
tap_failure_to_iterable(f)
Create function that applies a collections.abc.Iterable-returning side effect to trcks.Failure values.
trcks.SuccessTuple values are passed on without side effects.
Parameters:
-
f(Callable[[_F1], Iterable[object]]) –Side effect to apply to the trcks.Failure value.
Returns:
-
Callable[[ResultTuple[_F1, _S1]], SuccessTuple[_F1] | SuccessTuple[_S1]]–Applies the given side effect to trcks.Failure values and converts them to trcks.SuccessTuple values containing the original failure repeated once per element in the collections.abc.Iterable returned by the side effect. Passes on trcks.SuccessTuple values without side effects.
Example
>>> from collections.abc import Callable
>>> from trcks import ResultTuple, SuccessTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _log_and_alert(description: str) -> tuple[None, None]:
... return (
... print(f"Error logged: {description}"),
... print(f"Alert sent: {description}"),
... )
...
>>> log_and_alert: Callable[
... [ResultTuple[str, int]],
... SuccessTuple[str] | SuccessTuple[int],
... ] = rt.tap_failure_to_iterable(_log_and_alert)
>>> log_and_alert(("failure", "critical"))
Error logged: critical
Alert sent: critical
('success', ('critical', 'critical'))
>>> log_and_alert(("success", (1, 2)))
('success', (1, 2))
Source code in src/trcks/fp/monads/result_tuple.py
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 | |
tap_failure_to_result(f)
Create function that applies a side effect with return type trcks.Result to trcks.Failure values.
trcks.SuccessTuple values are passed on without side effects.
Parameters:
-
f(Callable[[_F1], Result[object, _S2]]) –Side effect to apply to the trcks.Failure value.
Returns:
-
Callable[[ResultTuple[_F1, _S1]], Result[_F1, tuple[_S1, ...] | tuple[_S2, ...]]]–Applies the given side effect to trcks.Failure values. If the given side effect returns a trcks.Failure, the original trcks.Failure is returned. If the given side effect returns a trcks.Success, this trcks.Success is returned (wrapped as a homogeneous tuple). Passes on trcks.SuccessTuple values without side effects.
Example
>>> from collections.abc import Callable
>>> from trcks import Result, ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _recover_from_not_found(description: str) -> Result[None, int]:
... if description == "not found":
... return "success", 42
... return "failure", None
>>> recover_from_not_found: Callable[
... [ResultTuple[str, int]], Result[str, tuple[int, ...]]
... ] = rt.tap_failure_to_result(_recover_from_not_found)
>>> recover_from_not_found(("failure", "not found"))
('success', (42,))
>>> recover_from_not_found(("failure", "fatal"))
('failure', 'fatal')
>>> recover_from_not_found(("success", (1, 2)))
('success', (1, 2))
Source code in src/trcks/fp/monads/result_tuple.py
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 | |
tap_failure_to_result_iterable(f)
Create function that applies a side effect with return type trcks.ResultIterable to trcks.Failure values.
trcks.SuccessTuple values are passed on without side effects.
Parameters:
-
f(Callable[[_F1], ResultIterable[object, _S2]]) –Side effect to apply to the trcks.Failure value.
Returns:
-
Callable[[ResultTuple[_F1, _S1]], Result[_F1, tuple[_S1, ...] | tuple[_S2, ...]]]–Applies the given side effect to trcks.Failure values. If the given side effect returns a trcks.Failure, the original trcks.Failure is returned. If the given side effect returns a trcks.SuccessIterable this trcks.SuccessIterable is returned. Passes on trcks.SuccessTuple values without side effects.
Example
>>> from collections.abc import Callable
>>> from trcks import Result, ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _recover_from_not_found(description: str) -> ResultTuple[None, int]:
... if description == "not found":
... return "success", (42,)
... return "failure", None
>>> recover_from_not_found: Callable[
... [ResultTuple[str, int]], Result[str, tuple[int, ...]]
... ] = rt.tap_failure_to_result_iterable(_recover_from_not_found)
>>> recover_from_not_found(("failure", "not found"))
('success', (42,))
>>> recover_from_not_found(("failure", "fatal"))
('failure', 'fatal')
>>> recover_from_not_found(("success", (1, 2)))
('success', (1, 2))
Source code in src/trcks/fp/monads/result_tuple.py
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 | |
tap_failure_to_result_tuple(f)
Deprecated alias for trcks.fp.monads.result_tuple.tap_failure_to_result_iterable.
Source code in src/trcks/fp/monads/result_tuple.py
677 678 679 680 681 682 683 684 | |
tap_failure_to_tuple(f)
Deprecated alias for trcks.fp.monads.result_tuple.tap_failure_to_iterable.
Source code in src/trcks/fp/monads/result_tuple.py
687 688 689 690 691 692 | |
tap_successes(f)
Create function that applies a side effect to each element of a trcks.SuccessTuple.
trcks.Failure values are passed on without side effects.
Parameters:
-
f(Callable[[_S1], object]) –Side effect to apply to each element of the trcks.SuccessTuple.
Returns:
-
Callable[[ResultTuple[_F1, _S1]], ResultTuple[_F1, _S1]]–Passes on trcks.Failure values without side effects. Applies the given side effect to each element of the trcks.SuccessTuple and returns the original trcks.SuccessTuple.
Example
>>> from collections.abc import Callable
>>> from trcks import ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _log_integer(n: int) -> None:
... print(f"Received: {n}")
...
>>> log_integers: Callable[
... [ResultTuple[str, int]], ResultTuple[str, int]
... ] = rt.tap_successes(_log_integer)
>>> r_tpl_1 = log_integers(("success", (1, 2)))
Received: 1
Received: 2
>>> r_tpl_1
('success', (1, 2))
>>> r_tpl_2 = log_integers(("failure", "oops"))
>>> r_tpl_2
('failure', 'oops')
Source code in src/trcks/fp/monads/result_tuple.py
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 | |
tap_successes_to_iterable(f)
Create function that applies a collections.abc.Iterable-returning side effect to each element of a trcks.SuccessTuple.
trcks.Failure values are passed on without side effects.
Parameters:
-
f(Callable[[_S1], Iterable[object]]) –Side effect to apply to each element of the trcks.SuccessTuple.
Returns:
-
Callable[[ResultTuple[_F1, _S1]], ResultTuple[_F1, _S1]]–Passes on trcks.Failure values without side effects. Applies the given side effect to each element of the trcks.SuccessTuple and repeats each original element once per element in the collections.abc.Iterable returned by the side effect.
Example
>>> from collections.abc import Callable
>>> from trcks import ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _log_twice(n: int) -> tuple[None, None]:
... return print(f"Received: {n}"), print(f"Received: {n}")
...
>>> log_twice: Callable[
... [ResultTuple[str, int]], ResultTuple[str, int]
... ] = rt.tap_successes_to_iterable(_log_twice)
>>> log_twice(("success", (7,)))
Received: 7
Received: 7
('success', (7, 7))
Source code in src/trcks/fp/monads/result_tuple.py
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 | |
tap_successes_to_result(f)
Create function that applies a side effect with return type trcks.Result to each element of a trcks.SuccessTuple.
trcks.Failure values are passed on without side effects.
Parameters:
-
f(Callable[[_S1], Result[_F2, object]]) –Side effect to apply to each element of the trcks.SuccessTuple.
Returns:
-
Callable[[ResultTuple[_F1, _S1]], ResultTuple[_F1 | _F2, _S1]]–Passes on trcks.Failure values without side effects. Applies the given side effect to each element of the trcks.SuccessTuple. If the given side effect returns a trcks.Failure, this trcks.Failure is returned. If the given side effect returns a trcks.Success, the original trcks.SuccessTuple element is returned.
Example
>>> from collections.abc import Callable
>>> from trcks import Result, ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _validate_positive(n: int) -> Result[str, None]:
... if n > 0:
... return "success", None
... return "failure", "not positive"
...
>>> validate_positive: Callable[
... [ResultTuple[str, int]], ResultTuple[str, int]
... ] = rt.tap_successes_to_result(_validate_positive)
>>> validate_positive(("success", (1, 2)))
('success', (1, 2))
>>> validate_positive(("success", (1, -1, 2)))
('failure', 'not positive')
>>> validate_positive(("failure", "oops"))
('failure', 'oops')
Source code in src/trcks/fp/monads/result_tuple.py
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 | |
tap_successes_to_result_iterable(f)
Create function that applies a side effect with return type trcks.ResultTuple to each element of a trcks.SuccessTuple.
trcks.Failure values are passed on without side effects.
Parameters:
-
f(Callable[[_S1], ResultIterable[_F2, object]]) –Side effect to apply to each element of the trcks.SuccessTuple.
Returns:
-
Callable[[ResultTuple[_F1, _S1]], ResultTuple[_F1 | _F2, _S1]]–Passes on trcks.Failure values without side effects. Applies the given side effect to each element of the trcks.SuccessTuple. If the given side effect returns a trcks.Failure, this trcks.Failure is returned. If the given side effect returns a trcks.SuccessIterable, the original trcks.SuccessTuple element is repeated once per element in the side effect output.
Example
>>> from collections.abc import Callable
>>> from trcks import ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _validate_positive_twice(n: int) -> ResultTuple[str, None]:
... if n > 0:
... return "success", (None, None)
... return "failure", "not positive"
...
>>> validate_positive_twice: Callable[
... [ResultTuple[str, int]], ResultTuple[str, int]
... ] = rt.tap_successes_to_result_iterable(_validate_positive_twice)
>>> validate_positive_twice(("success", (7,)))
('success', (7, 7))
>>> validate_positive_twice(("success", (1, -1)))
('failure', 'not positive')
Source code in src/trcks/fp/monads/result_tuple.py
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 | |
tap_successes_to_result_tuple(f)
Deprecated alias for trcks.fp.monads.result_tuple.tap_successes_to_result_iterable.
Source code in src/trcks/fp/monads/result_tuple.py
868 869 870 871 872 873 874 875 | |
tap_successes_to_tuple(f)
Deprecated alias for trcks.fp.monads.result_tuple.tap_successes_to_iterable.
Source code in src/trcks/fp/monads/result_tuple.py
878 879 880 881 882 883 884 885 | |