@@ -150,6 +150,38 @@ def is_ok(self) -> bool:
150
150
"""Return `True` if the result is an `Ok` value."""
151
151
return self .tag == "ok"
152
152
153
+ def filter (self , predicate : Callable [[_TSource ], bool ], default : _TError ) -> Result [_TSource , _TError ]:
154
+ """Filter result.
155
+
156
+ Returns the input if the predicate evaluates to true, otherwise
157
+ returns the `default`
158
+ """
159
+ match self :
160
+ case Result (tag = "ok" , ok = value ) if predicate (value ):
161
+ return self
162
+ case Result (tag = "error" ):
163
+ return self
164
+ case _:
165
+ return Error (default )
166
+
167
+ def filter_with (
168
+ self ,
169
+ predicate : Callable [[_TSource ], bool ],
170
+ default : Callable [[_TSource ], _TError ],
171
+ ) -> Result [_TSource , _TError ]:
172
+ """Filter result.
173
+
174
+ Returns the input if the predicate evaluates to true, otherwise
175
+ returns the `default` using the value as input
176
+ """
177
+ match self :
178
+ case Result (tag = "ok" , ok = value ) if predicate (value ):
179
+ return self
180
+ case Result (tag = "ok" , ok = value ):
181
+ return Error (default (value ))
182
+ case Result ():
183
+ return self
184
+
153
185
def dict (self ) -> builtins .dict [str , _TSource | _TError | Literal ["ok" , "error" ]]:
154
186
"""Return a json serializable representation of the result."""
155
187
match self :
@@ -352,6 +384,11 @@ def map2(
352
384
return x .map2 (y , mapper )
353
385
354
386
387
+ @curry_flip (1 )
388
+ def map_error (result : Result [_TSource , _TError ], mapper : Callable [[_TError ], _TResult ]) -> Result [_TSource , _TResult ]:
389
+ return result .map_error (mapper )
390
+
391
+
355
392
@curry_flip (1 )
356
393
def bind (
357
394
result : Result [_TSource , _TError ],
@@ -374,11 +411,46 @@ def is_error(result: Result[_TSource, _TError]) -> TypeGuard[Result[_TSource, _T
374
411
return result .is_error ()
375
412
376
413
414
+ @curry_flip (1 )
415
+ def filter (
416
+ result : Result [_TSource , _TError ],
417
+ predicate : Callable [[_TSource ], bool ],
418
+ default : _TError ,
419
+ ) -> Result [_TSource , _TError ]:
420
+ return result .filter (predicate , default )
421
+
422
+
423
+ @curry_flip (1 )
424
+ def filter_with (
425
+ result : Result [_TSource , _TError ],
426
+ predicate : Callable [[_TSource ], bool ],
427
+ default : Callable [[_TSource ], _TError ],
428
+ ) -> Result [_TSource , _TError ]:
429
+ return result .filter_with (predicate , default )
430
+
431
+
377
432
def swap (result : Result [_TSource , _TError ]) -> Result [_TError , _TSource ]:
378
433
"""Swaps the value in the result so an Ok becomes an Error and an Error becomes an Ok."""
379
434
return result .swap ()
380
435
381
436
437
+ @curry_flip (1 )
438
+ def or_else (result : Result [_TSource , _TError ], other : Result [_TSource , _TError ]) -> Result [_TSource , _TError ]:
439
+ return result .or_else (other )
440
+
441
+
442
+ @curry_flip (1 )
443
+ def or_else_with (
444
+ result : Result [_TSource , _TError ],
445
+ other : Callable [[_TError ], Result [_TSource , _TError ]],
446
+ ) -> Result [_TSource , _TError ]:
447
+ return result .or_else_with (other )
448
+
449
+
450
+ def merge (result : Result [_TSource , _TSource ]) -> _TSource :
451
+ return result .merge ()
452
+
453
+
382
454
def to_option (result : Result [_TSource , Any ]) -> Option [_TSource ]:
383
455
from expression .core .option import Nothing , Some
384
456
@@ -406,9 +478,17 @@ def of_option_with(value: Option[_TSource], error: Callable[[], _TError]) -> Res
406
478
"map" ,
407
479
"bind" ,
408
480
"dict" ,
481
+ "filter" ,
482
+ "filter_with" ,
409
483
"is_ok" ,
410
484
"is_error" ,
485
+ "map2" ,
486
+ "map_error" ,
487
+ "merge" ,
411
488
"to_option" ,
412
489
"of_option" ,
413
490
"of_option_with" ,
491
+ "or_else" ,
492
+ "or_else_with" ,
493
+ "swap" ,
414
494
]
0 commit comments