mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-07 19:55:31 -05:00
feat(CCList): update with regards to partition_map
- Add partition_filter_map - Deprecate partition_map - Add partition_map_either that match the partition_map from the std
This commit is contained in:
parent
30121b8d2c
commit
6b52ec6945
3 changed files with 53 additions and 6 deletions
|
|
@ -530,7 +530,28 @@ let diagonal l =
|
||||||
diagonal [1;2;3] |> List.sort Stdlib.compare = [1, 2; 1, 3; 2, 3]
|
diagonal [1;2;3] |> List.sort Stdlib.compare = [1, 2; 1, 3; 2, 3]
|
||||||
*)
|
*)
|
||||||
|
|
||||||
let partition_map f l =
|
let partition_filter_either f l =
|
||||||
|
let rec iter f l1 l2 l = match l with
|
||||||
|
| [] -> List.rev l1, List.rev l2
|
||||||
|
| x :: tl ->
|
||||||
|
match f x with
|
||||||
|
| CCEither.Left y -> iter f (y :: l1) l2 tl
|
||||||
|
| CCEither.Right y -> iter f l1 (y :: l2) tl
|
||||||
|
in
|
||||||
|
iter f [] [] l
|
||||||
|
|
||||||
|
(*$R
|
||||||
|
let l1, l2 =
|
||||||
|
partition_filter_either (function
|
||||||
|
| n when n mod 2 = 0 -> CCEither.Left n
|
||||||
|
| n -> CCEither.Right n
|
||||||
|
) [0;1;2;3;4]
|
||||||
|
in
|
||||||
|
assert_equal [0;2;4] l1;
|
||||||
|
assert_equal [1;3] l2
|
||||||
|
*)
|
||||||
|
|
||||||
|
let partition_filter_map f l =
|
||||||
let rec iter f l1 l2 l = match l with
|
let rec iter f l1 l2 l = match l with
|
||||||
| [] -> List.rev l1, List.rev l2
|
| [] -> List.rev l1, List.rev l2
|
||||||
| x :: tl ->
|
| x :: tl ->
|
||||||
|
|
@ -541,9 +562,11 @@ let partition_map f l =
|
||||||
in
|
in
|
||||||
iter f [] [] l
|
iter f [] [] l
|
||||||
|
|
||||||
|
let partition_map = partition_filter_map
|
||||||
|
|
||||||
(*$R
|
(*$R
|
||||||
let l1, l2 =
|
let l1, l2 =
|
||||||
partition_map (function
|
partition_filter_map (function
|
||||||
| n when n = 0 -> `Drop
|
| n when n = 0 -> `Drop
|
||||||
| n when n mod 2 = 0 -> `Left n
|
| n when n mod 2 = 0 -> `Left n
|
||||||
| n -> `Right n
|
| n -> `Right n
|
||||||
|
|
|
||||||
|
|
@ -231,14 +231,26 @@ val diagonal : 'a t -> ('a * 'a) t
|
||||||
(** [diagonal l] returns all pairs of distinct positions of the list [l],
|
(** [diagonal l] returns all pairs of distinct positions of the list [l],
|
||||||
that is the list of [List.nth i l, List.nth j l] if [i < j]. *)
|
that is the list of [List.nth i l, List.nth j l] if [i < j]. *)
|
||||||
|
|
||||||
val partition_map : ('a -> [<`Left of 'b | `Right of 'c | `Drop]) ->
|
val partition_filter_either : ('a -> ('b, 'c) CCEither.t) ->
|
||||||
'a list -> 'b list * 'c list
|
'a list -> 'b list * 'c list
|
||||||
(** [partition_map f l] maps [f] on [l] and gather results in lists:
|
(** [partition_filter_either f l] maps [f] on [l] and gather results in lists:
|
||||||
|
- if [f x = Left y], adds [y] to the first list.
|
||||||
|
- if [f x = Right z], adds [z] to the second list.
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
|
val partition_filter_map : ('a -> [<`Left of 'b | `Right of 'c | `Drop]) ->
|
||||||
|
'a list -> 'b list * 'c list
|
||||||
|
(** [partition_filter_map f l] maps [f] on [l] and gather results in lists:
|
||||||
- if [f x = `Left y], adds [y] to the first list.
|
- if [f x = `Left y], adds [y] to the first list.
|
||||||
- if [f x = `Right z], adds [z] to the second list.
|
- if [f x = `Right z], adds [z] to the second list.
|
||||||
- if [f x = `Drop], ignores [x].
|
- if [f x = `Drop], ignores [x].
|
||||||
@since 0.11 *)
|
@since 0.11 *)
|
||||||
|
|
||||||
|
val partition_map : ('a -> [<`Left of 'b | `Right of 'c | `Drop]) ->
|
||||||
|
'a list -> 'b list * 'c list
|
||||||
|
[@@ocaml.deprecated "use CCList.partition_filter_map instead"]
|
||||||
|
(** @deprecated use {!partition_filter_map} instead *)
|
||||||
|
|
||||||
val group_by : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) ->
|
val group_by : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) ->
|
||||||
'a t -> 'a list t
|
'a t -> 'a list t
|
||||||
(** [group_by ?hash ?eq l] groups equal elements, regardless of their order of appearance.
|
(** [group_by ?hash ?eq l] groups equal elements, regardless of their order of appearance.
|
||||||
|
|
|
||||||
|
|
@ -235,14 +235,26 @@ val diagonal : 'a t -> ('a * 'a) t
|
||||||
(** [diagonal l] returns all pairs of distinct positions of the list [l],
|
(** [diagonal l] returns all pairs of distinct positions of the list [l],
|
||||||
that is the list of [List.nth i l, List.nth j l] if [i < j]. *)
|
that is the list of [List.nth i l, List.nth j l] if [i < j]. *)
|
||||||
|
|
||||||
val partition_map : f:('a -> [<`Left of 'b | `Right of 'c | `Drop]) ->
|
val partition_filter_either : f:('a -> ('b, 'c) CCEither.t) ->
|
||||||
'a list -> 'b list * 'c list
|
'a list -> 'b list * 'c list
|
||||||
(** [partition_map ~f l] maps [f] on [l] and gather results in lists:
|
(** [partition_filter_either ~f l] maps [f] on [l] and gather results in lists:
|
||||||
|
- if [f x = Left y], adds [y] to the first list.
|
||||||
|
- if [f x = Right z], adds [z] to the second list.
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
|
val partition_filter_map : f:('a -> [<`Left of 'b | `Right of 'c | `Drop]) ->
|
||||||
|
'a list -> 'b list * 'c list
|
||||||
|
(** [partition_filter_map ~f l] maps [f] on [l] and gather results in lists:
|
||||||
- if [f x = `Left y], adds [y] to the first list.
|
- if [f x = `Left y], adds [y] to the first list.
|
||||||
- if [f x = `Right z], adds [z] to the second list.
|
- if [f x = `Right z], adds [z] to the second list.
|
||||||
- if [f x = `Drop], ignores [x].
|
- if [f x = `Drop], ignores [x].
|
||||||
@since 0.11 *)
|
@since 0.11 *)
|
||||||
|
|
||||||
|
val partition_map : f:('a -> [<`Left of 'b | `Right of 'c | `Drop]) ->
|
||||||
|
'a list -> 'b list * 'c list
|
||||||
|
[@@ocaml.deprecated "use CCList.partition_filter_map instead"]
|
||||||
|
(** @deprecated use {!partition_filter_map} instead *)
|
||||||
|
|
||||||
val group_by : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) ->
|
val group_by : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) ->
|
||||||
'a t -> 'a list t
|
'a t -> 'a list t
|
||||||
(** [group_by ?hash ?eq l] groups equal elements, regardless of their order of appearance.
|
(** [group_by ?hash ?eq l] groups equal elements, regardless of their order of appearance.
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue