You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+193-4Lines changed: 193 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,6 +21,7 @@ The `ResultContainer` is designed to streamline error propagation and improve co
21
21
-**Variants for Success and Failure**: Two variants represented in a Result instance, `Ok(value)` for successful outcomes, and `Err(e)` for errors that have resulted. Provides a flexible mechanism for chaining operations on the `Ok` value while propagating errors through `Err`.
22
22
-**Attribute and Method Transparency**: Automatically passes attributes, methods, indices, and math operations to the value contained within an `Ok`, otherwise propagates the `Err(e)`.
23
23
-**Utility Methods**: Implements helper methods for error propagation, transformation, and querying (e.g., `.map()`, `.apply()`, `.unwrap_or()`, `.expect()`, `.raises()`) for concise and readable handling of success and error cases.
24
+
-**Functional Programming**: Provides an easy way to track errors when method chaining.
24
25
25
26
## Installation
26
27
To install the module
@@ -432,7 +433,7 @@ bad_dt.raises() # raises a ResultErr exception
432
433
433
434
434
435
435
-
### Passing Functions and Chaining Operations
436
+
### Functions
436
437
437
438
```python
438
439
from ResultContainer import Result, Ok, Err
@@ -446,12 +447,200 @@ c = Ok(-9) # Ok(-9)
446
447
d = c.apply(sqrt) # Err("Result.apply exception. | ValueError: math domain error")
447
448
e = sqrt(c.expect()) # raises an error
448
449
450
+
451
+
```
452
+
453
+
454
+
455
+
### Method Chaining
456
+
457
+
```python
458
+
from ResultContainer import Result, Ok, Err
459
+
from math import sqrt
460
+
449
461
plus1 =lambdax: x +1
462
+
a = Ok(2)
463
+
b = (a /0).map_or(10, plus1).map_or(20, plus1).map_or(30, plus1) # Err(div/0) -> Ok(10) -> Ok(11) -> Ok(12)
464
+
465
+
```
466
+
467
+
468
+
469
+
470
+
#### Function Chaining
471
+
472
+
```python
473
+
from ResultContainer import Result, Ok, Err
474
+
from math import sqrt
475
+
476
+
# Some functions include `*args` because `apply_or_else(err_func, ok_func)` requires
477
+
# that both the err_func and ok_func have the same argument length.
478
+
# The *args serves as a place holder to catch the unused extra argument.
479
+
#
480
+
# For example: `Ok(10).apply_or_else(plus11, div, 2)`
481
+
# first evaluates: `div(10, 2)`
482
+
# and if the function fails, tries: `plus11(10, 2)`
483
+
# where the `2` must be passed, but is not part of the method
484
+
485
+
defdiv(x, y): return x / y
486
+
487
+
defpow2(x): return x**2
488
+
489
+
defplus11(x, *args): return x +11# *args is ignored
0 commit comments