From 8d8f1d41459a090f518ddee90860dc4dcb67070b Mon Sep 17 00:00:00 2001 From: seprov <95832216+seprov@users.noreply.github.com> Date: Fri, 15 Nov 2024 10:01:20 -0600 Subject: [PATCH] Add `split_result` (#459) add `CCList.split_result` (@seprov) --- src/core/CCList.ml | 7 +++++++ src/core/CCList.mli | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/src/core/CCList.ml b/src/core/CCList.ml index 87c7fefc..fa82eaeb 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -1048,6 +1048,13 @@ let all_ok l = | None -> assert false | Some e -> Error e) +let split_result results = + results + |> partition_filter_map (fun x -> + match x with + | Ok o -> `Left o + | Error e -> `Right e) + let group_by (type k) ?(hash = Hashtbl.hash) ?(eq = Stdlib.( = )) l = let module Tbl = Hashtbl.Make (struct type t = k diff --git a/src/core/CCList.mli b/src/core/CCList.mli index b9507f77..9182b749 100644 --- a/src/core/CCList.mli +++ b/src/core/CCList.mli @@ -495,6 +495,10 @@ val all_ok : ('a, 'err) result t -> ('a t, 'err) result @since 1.3, but only @since 2.2 with labels *) +val split_result : ('ok, 'error) result list -> 'ok list * 'error list +(** Split a list of results into [Ok]s and [Error]s. + @since NEXT_RELEASE *) + val sorted_mem : cmp:('a -> 'a -> int) -> 'a -> 'a list -> bool (** [sorted_mem ~cmp x l] and [mem x l] give the same result for any sorted list [l], but potentially more efficiently.