add CCResult.fold_ok (closes #107)

This commit is contained in:
Simon Cruanes 2017-04-05 10:43:37 +02:00
parent ee69bdcab8
commit 02b2a21e33
2 changed files with 15 additions and 2 deletions

View file

@ -114,6 +114,15 @@ let fold ~ok ~error x = match x with
| Ok x -> ok x
| Error s -> error s
let fold_ok f acc r = match r with
| Ok x -> f acc x
| Error _ -> acc
(*$=
42 (fold_ok (+) 2 (Ok 40))
40 (fold_ok (+) 40 (Error "foo"))
*)
let is_ok = function
| Ok _ -> true
| Error _ -> false

View file

@ -101,9 +101,13 @@ val fold : ok:('a -> 'b) -> error:('err -> 'b) -> ('a, 'err) t -> 'b
(** [fold ~ok ~error e] opens [e] and, if [e = Ok x], returns
[ok x], otherwise [e = Error s] and it returns [error s]. *)
val is_ok : ('a, 'err) t -> bool
(** Return true if Ok/
val fold_ok : ('a -> 'b -> 'a) -> 'a -> ('b, _) t -> 'a
(** [fold_ok f acc r] will compute [f acc x] if [r=Ok x],
and return [acc] otherwise, as if the result were a mere option.
@since NEXT_RELEASE *)
val is_ok : ('a, 'err) t -> bool
(** Return true if Ok
@since 1.0 *)
val is_error : ('a, 'err) t -> bool