Add CCResult.iter_err

Closes #238
This commit is contained in:
Nathan Rebours 2018-11-19 17:41:20 +01:00 committed by Simon Cruanes
parent da2c9e7c7c
commit 29a75daac1
3 changed files with 18 additions and 0 deletions

View file

@ -33,3 +33,4 @@
- Metin Akat (@loxs)
- Francois Berenger (@UnixJunkie)
- Hongchang Wu (@hongchangwu)
- Nathan Rebours (@NathanReb)

View file

@ -83,6 +83,19 @@ let iter f e = match e with
| Ok x -> f x
| Error _ -> ()
let iter_err f e = match e with
| Ok _ -> ()
| Error err -> f err
(*$R iter_err
let called_with = ref None in
let f e = called_with := Some e in
iter_err f (Ok 1);
assert_bool "should not apply when Ok" (!called_with = None);
iter_err f (Error 1);
assert_bool "should apply f to Error" (!called_with = Some 1)
*)
exception Get_error
let get_exn = function

View file

@ -73,6 +73,10 @@ val map2 : ('a -> 'b) -> ('err1 -> 'err2) -> ('a, 'err1) t -> ('b, 'err2) t
val iter : ('a -> unit) -> ('a, _) t -> unit
(** Apply the function only in case of [Ok]. *)
val iter_err : ('err -> unit) -> (_, 'err) t -> unit
(** Apply the function in case of [Error].
@since NEXT_RELEASE *)
exception Get_error
val get_exn : ('a, _) t -> 'a