From 4c9ab1c659fab9d498ee578e1f6324fb8518cec1 Mon Sep 17 00:00:00 2001 From: Sebastian Provenzano Date: Thu, 14 Nov 2024 17:41:35 -0600 Subject: [PATCH] add function --- src/core/CCResult.ml | 5 +++++ src/core/CCResult.mli | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/src/core/CCResult.ml b/src/core/CCResult.ml index 69bbab60..2a99f726 100644 --- a/src/core/CCResult.ml +++ b/src/core/CCResult.ml @@ -167,6 +167,11 @@ let is_error = function | Ok _ -> false | Error _ -> true +let split_ok_error results = + results + |> CCList.partition_filter_map (fun x -> + match x with Ok o -> `Left o | Error e -> `Right e) + (** {2 Wrappers} *) let guard f = try Ok (f ()) with e -> Error e diff --git a/src/core/CCResult.mli b/src/core/CCResult.mli index 5347b3fd..21e339e4 100644 --- a/src/core/CCResult.mli +++ b/src/core/CCResult.mli @@ -145,6 +145,10 @@ val is_error : ('a, 'err) t -> bool (** Return true if [Error]. @since 1.0 *) +val split_ok_error : ('ok, 'error) result list -> 'ok list * 'error list +(** Split a list of results into [Ok]s and [Error]s. + @since 3.14.1 *) + (** {2 Wrappers} *) val guard : (unit -> 'a) -> ('a, exn) t