mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-12 14:00:33 -05:00
add CCResult.{map_or,get_or}
This commit is contained in:
parent
86f170f213
commit
16ac701de1
2 changed files with 14 additions and 0 deletions
|
|
@ -64,6 +64,14 @@ let get_exn = function
|
||||||
| Ok x -> x
|
| Ok x -> x
|
||||||
| Error _ -> raise Get_error
|
| Error _ -> raise Get_error
|
||||||
|
|
||||||
|
let get_or e ~default = match e with
|
||||||
|
| Ok x -> x
|
||||||
|
| Error _ -> default
|
||||||
|
|
||||||
|
let map_or f e ~default = match e with
|
||||||
|
| Ok x -> f x
|
||||||
|
| Error _ -> default
|
||||||
|
|
||||||
let catch e ~ok ~err = match e with
|
let catch e ~ok ~err = match e with
|
||||||
| Ok x -> ok x
|
| Ok x -> ok x
|
||||||
| Error y -> err y
|
| Error y -> err y
|
||||||
|
|
|
||||||
|
|
@ -64,6 +64,12 @@ val get_exn : ('a, _) t -> 'a
|
||||||
whenever possible.
|
whenever possible.
|
||||||
@raise Get_error if the value is an error. *)
|
@raise Get_error if the value is an error. *)
|
||||||
|
|
||||||
|
val get_or : ('a, _) t -> default:'a -> 'a
|
||||||
|
(** [get_or e ~default] returns [x] if [e = Ok x], [default] otherwise *)
|
||||||
|
|
||||||
|
val map_or : ('a -> 'b) -> ('a, 'b) t -> default:'b -> 'b
|
||||||
|
(** [map_or f e ~default] returns [f x] if [e = Ok x], [default] otherwise *)
|
||||||
|
|
||||||
val catch : ('a, 'err) t -> ok:('a -> 'b) -> err:('err -> 'b) -> 'b
|
val catch : ('a, 'err) t -> ok:('a -> 'b) -> err:('err -> 'b) -> 'b
|
||||||
(** [catch e ~ok ~err] calls either [ok] or [err] depending on
|
(** [catch e ~ok ~err] calls either [ok] or [err] depending on
|
||||||
the value of [e]. *)
|
the value of [e]. *)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue