From e6dd5db678f14eacf151de629897279b6b5b8f61 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Fri, 27 Jun 2014 15:25:34 +0200 Subject: [PATCH] CCError.map2 --- core/CCError.ml | 4 ++++ core/CCError.mli | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/core/CCError.ml b/core/CCError.ml index 8418f21f..da7f7888 100644 --- a/core/CCError.ml +++ b/core/CCError.ml @@ -49,6 +49,10 @@ let map f e = match e with | `Ok x -> `Ok (f x) | `Error s -> `Error s +let map2 f g e = match e with + | `Ok x -> `Ok (f x) + | `Error s -> `Error (g s) + 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 49462101..61e8e98a 100644 --- a/core/CCError.mli +++ b/core/CCError.mli @@ -47,6 +47,10 @@ val of_exn : exn -> 'a t val map : ('a -> 'b) -> 'a t -> 'b t +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 flat_map : ('a -> 'b t) -> 'a t -> 'b t val guard : (unit -> 'a) -> 'a t