From 29a75daac1df53e83a19b930b3b703a58114c39c Mon Sep 17 00:00:00 2001 From: Nathan Rebours Date: Mon, 19 Nov 2018 17:41:20 +0100 Subject: [PATCH] Add CCResult.iter_err Closes #238 --- AUTHORS.adoc | 1 + src/core/CCResult.ml | 13 +++++++++++++ src/core/CCResult.mli | 4 ++++ 3 files changed, 18 insertions(+) diff --git a/AUTHORS.adoc b/AUTHORS.adoc index 52aa14d6..21969b62 100644 --- a/AUTHORS.adoc +++ b/AUTHORS.adoc @@ -33,3 +33,4 @@ - Metin Akat (@loxs) - Francois Berenger (@UnixJunkie) - Hongchang Wu (@hongchangwu) +- Nathan Rebours (@NathanReb) diff --git a/src/core/CCResult.ml b/src/core/CCResult.ml index 12205866..05a23f23 100644 --- a/src/core/CCResult.ml +++ b/src/core/CCResult.ml @@ -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 diff --git a/src/core/CCResult.mli b/src/core/CCResult.mli index c50c6b81..6e4f2333 100644 --- a/src/core/CCResult.mli +++ b/src/core/CCResult.mli @@ -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