feat(CCResult): add get_lazy

fixes #285
This commit is contained in:
Fardale 2020-01-24 22:57:25 +01:00
parent 9d083df3a6
commit d1a5e047fe
2 changed files with 13 additions and 0 deletions

View file

@ -117,6 +117,15 @@ let get_or_failwith = function
try ignore @@ get_or_failwith (Error "e"); false with Failure msg -> msg = "e"
*)
let get_lazy default_fn x = match x with
| Ok x -> x
| Error e -> default_fn e
(*$= get_lazy
(get_lazy (fun _ -> 2) (Ok 1)) (1)
(get_lazy (fun _ -> 2) (Error "error")) (2)
*)
let map_or f e ~default = match e with
| Ok x -> f x
| Error _ -> default

View file

@ -101,6 +101,10 @@ val get_or_failwith : ('a, string) t -> 'a
@raise Failure with [msg] if [e = Error msg].
@since 2.4 *)
val get_lazy : ('b -> 'a) -> ('a, 'b) t -> 'a
(** [get_lazy default_fn x] unwraps [x], but if [x = Error e] it returns [default_fr e] instead.
@since NEXT_RELEASE *)
val map_or : ('a -> 'b) -> ('a, 'c) t -> default:'b -> 'b
(** [map_or f e ~default] returns [f x] if [e = Ok x], [default] otherwise. *)