applicative and lifting operators for CCError

This commit is contained in:
Simon Cruanes 2014-06-27 15:33:57 +02:00
parent e6dd5db678
commit f64002b053
2 changed files with 43 additions and 7 deletions

View file

@ -57,11 +57,6 @@ let flat_map f e = match e with
| `Ok x -> f x
| `Error s -> `Error s
let guard f =
try
return (f ())
with e -> of_exn e
let (>|=) e f = map f e
let (>>=) e f = flat_map f e
@ -81,6 +76,33 @@ let fold ~success ~failure x = match x with
| `Ok x -> success x
| `Error s -> failure s
(** {2 Wrappers} *)
let guard f =
try
return (f ())
with e -> of_exn e
let wrap1 f x =
try return (f x)
with e -> of_exn e
let wrap2 f x y =
try return (f x y)
with e -> of_exn e
let wrap3 f x y z =
try return (f x y z)
with e -> of_exn e
(** {2 Applicative} *)
let pure = return
let (<*>) f x = match f with
| `Error s -> fail s
| `Ok f -> map f x
(** {2 Collections} *)
let map_l f l =

View file

@ -53,8 +53,6 @@ val map2 : ('a -> 'b) -> (string -> string) -> 'a t -> 'b t
val flat_map : ('a -> 'b t) -> 'a t -> 'b t
val guard : (unit -> 'a) -> 'a t
val (>|=) : 'a t -> ('a -> 'b) -> 'b t
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
@ -67,6 +65,22 @@ val fold : success:('a -> 'b) -> failure:(string -> 'b) -> 'a t -> 'b
(** [fold ~success ~failure e] opens [e] and, if [e = `Ok x], returns
[success x], otherwise [e = `Error s] and it returns [failure s]. *)
(** {2 Wrappers} *)
val guard : (unit -> 'a) -> 'a t
val wrap1 : ('a -> 'b) -> 'a -> 'b t
val wrap2 : ('a -> 'b -> 'c) -> 'a -> 'b -> 'c t
val wrap3 : ('a -> 'b -> 'c -> 'd) -> 'a -> 'b -> 'c -> 'd t
(** {2 APplicative} *)
val pure : 'a -> 'a t
val (<*>) : ('a -> 'b) t -> 'a t -> 'b t
(** {2 Collections} *)
val map_l : ('a -> 'b t) -> 'a list -> 'b list t