mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
parent
9d083df3a6
commit
d1a5e047fe
2 changed files with 13 additions and 0 deletions
|
|
@ -117,6 +117,15 @@ let get_or_failwith = function
|
||||||
try ignore @@ get_or_failwith (Error "e"); false with Failure msg -> msg = "e"
|
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
|
let map_or f e ~default = match e with
|
||||||
| Ok x -> f x
|
| Ok x -> f x
|
||||||
| Error _ -> default
|
| Error _ -> default
|
||||||
|
|
|
||||||
|
|
@ -101,6 +101,10 @@ val get_or_failwith : ('a, string) t -> 'a
|
||||||
@raise Failure with [msg] if [e = Error msg].
|
@raise Failure with [msg] if [e = Error msg].
|
||||||
@since 2.4 *)
|
@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
|
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. *)
|
(** [map_or f e ~default] returns [f x] if [e = Ok x], [default] otherwise. *)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue