Add split_result (#459)

add `CCList.split_result` (@seprov)
This commit is contained in:
seprov 2024-11-15 10:01:20 -06:00 committed by GitHub
parent 9cfedad7ba
commit 8d8f1d4145
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 0 deletions

View file

@ -1048,6 +1048,13 @@ let all_ok l =
| None -> assert false | None -> assert false
| Some e -> Error e) | 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 group_by (type k) ?(hash = Hashtbl.hash) ?(eq = Stdlib.( = )) l =
let module Tbl = Hashtbl.Make (struct let module Tbl = Hashtbl.Make (struct
type t = k type t = k

View file

@ -495,6 +495,10 @@ val all_ok : ('a, 'err) result t -> ('a t, 'err) result
@since 1.3, but only @since 1.3, but only
@since 2.2 with labels *) @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 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], (** [sorted_mem ~cmp x l] and [mem x l] give the same result for any sorted list [l],
but potentially more efficiently. but potentially more efficiently.