diff --git a/core/CCError.ml b/core/CCError.ml index eb8990f6..abe716f7 100644 --- a/core/CCError.ml +++ b/core/CCError.ml @@ -72,6 +72,14 @@ let map2 f g e = match e with | `Ok x -> `Ok (f x) | `Error s -> `Error (g s) +let iter f e = match e with + | `Ok x -> f x + | `Error _ -> () + +let get_exn = function + | `Ok x -> x + | `Error _ -> raise (Invalid_argument "CCError.get_exn") + let flat_map f e = match e with | `Ok x -> f x | `Error s -> `Error s diff --git a/core/CCError.mli b/core/CCError.mli index 3fefdcb3..ee2368dd 100644 --- a/core/CCError.mli +++ b/core/CCError.mli @@ -56,6 +56,15 @@ val map2 : ('a -> 'b) -> (string -> string) -> 'a t -> 'b t (** Same as {!map}, but also with a function that can transform the error message in case of failure *) +val iter : ('a -> unit) -> 'a t -> unit +(** Apply the function only in case of `Ok *) + +val get_exn : 'a t -> 'a +(** Extract the value [x] from [`Ok x], fails otherwise. + You should be careful with this function, and favor other combinators + whenever possible. + @raise Invalid_argument if the value is an error. *) + val flat_map : ('a -> 'b t) -> 'a t -> 'b t val (>|=) : 'a t -> ('a -> 'b) -> 'b t