join / both applicative functions for CCError

This commit is contained in:
carm 2015-11-22 17:04:24 -05:00
parent 4d9b1d68ed
commit eedce68653
2 changed files with 23 additions and 1 deletions

View file

@ -162,6 +162,17 @@ let (<*>) f x = match f with
| `Error s -> fail s | `Error s -> fail s
| `Ok f -> map f x | `Ok f -> map f x
let join t = match t with
| `Ok (`Ok o) -> `Ok o
| `Ok (`Error e) -> `Error e
| (`Error _) as e -> e
let both x y =
match x,y with
| `Ok o, `Ok o' -> `Ok (o, o')
| `Ok _, `Error e -> `Error e
| `Error e, _ -> `Error e
(** {2 Collections} *) (** {2 Collections} *)
let map_l f l = let map_l f l =

View file

@ -141,7 +141,18 @@ val pure : 'a -> ('a, 'err) t
val (<*>) : ('a -> 'b, 'err) t -> ('a, 'err) t -> ('b, 'err) t val (<*>) : ('a -> 'b, 'err) t -> ('a, 'err) t -> ('b, 'err) t
(** [a <*> b] evaluates [a] and [b], and, in case of success, returns (** [a <*> b] evaluates [a] and [b], and, in case of success, returns
[`Ok (a b)]. Otherwise, it fails, and the error of [a] is chosen [`Ok (a b)]. Otherwise, it fails, and the error of [a] is chosen
over the error of [b] if both fail *) over the error of [b] if both fail. *)
val join : (('a, 'err) t, 'err) t -> ('a, 'err) t
(** [join t], in case of success, returns [`Ok o] from [`Ok (`Ok o)]. Otherwise,
it fails with [`Error e] where [e] is the unwrapped error of [t].
@since NEXT_RELEASE *)
val both : ('a, 'err) t -> ('b, 'err) t -> (('a * 'b), 'err) t
(** [both a b], in case of success, returns [`Ok (o, o')] with the ok values
of [a] and [b]. Otherwise, it fails, and the error of [a] is chosen over the
error of [b] if both fail.
@since NEXT_RELEASE *)
(** {2 Infix} (** {2 Infix}