mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-09 04:35:29 -05:00
group_by now uses lists; a few more utils
This commit is contained in:
parent
6ec499799c
commit
86cd5c0e8d
2 changed files with 24 additions and 14 deletions
|
|
@ -260,25 +260,16 @@ module Map = struct
|
|||
end
|
||||
| {eq=Some eq; hash=Some hash; _} -> make_hash ~eq ~hash ()
|
||||
|
||||
let multiset build seq =
|
||||
let multimap build seq =
|
||||
seq (fun (k,v) ->
|
||||
build.update k (function
|
||||
| None -> Some [v]
|
||||
| Some l -> Some (v::l)));
|
||||
{ is_empty = build.cur.is_empty;
|
||||
size = build.cur.size;
|
||||
get = (fun k -> match build.cur.get k with
|
||||
| None -> None
|
||||
| Some v -> Some (Coll.of_list v));
|
||||
fold = (fun f acc ->
|
||||
build.cur.fold (fun acc k v -> f acc k (Coll.of_list v)) acc);
|
||||
to_seq = build.cur.to_seq
|
||||
|> CCSequence.map (fun (k,v) -> k,Coll.of_list v);
|
||||
}
|
||||
build.cur
|
||||
|
||||
let multimap_cmp ?cmp seq =
|
||||
let build = make_cmp ?cmp () in
|
||||
multiset build seq
|
||||
multimap build seq
|
||||
|
||||
let count build seq =
|
||||
seq (fun x ->
|
||||
|
|
@ -330,7 +321,7 @@ type (_, _) unary =
|
|||
| Contains : 'a equal * 'a -> ('a collection, bool) unary
|
||||
| Get : ('b,'c) safety * 'a -> (('a,'b) Map.t, 'c) unary
|
||||
| GroupBy : 'b ord * ('a -> 'b)
|
||||
-> ('a collection, ('b,'a collection) Map.t) unary
|
||||
-> ('a collection, ('b,'a list) Map.t) unary
|
||||
| Count : 'a ord -> ('a collection, ('a, int) Map.t) unary
|
||||
|
||||
type ('a,'b,'key,'c) join_descr = {
|
||||
|
|
@ -576,9 +567,15 @@ let map_to_list q =
|
|||
let group_by ?(cmp=Pervasives.compare) f q =
|
||||
Unary (GroupBy (cmp,f), q)
|
||||
|
||||
let group_by' ?cmp f q =
|
||||
map_iter (group_by ?cmp f q)
|
||||
|
||||
let count ?(cmp=Pervasives.compare) () q =
|
||||
Unary (Count cmp, q)
|
||||
|
||||
let count' ?cmp () q =
|
||||
map_iter (count ?cmp () q)
|
||||
|
||||
let fold f acc q =
|
||||
Unary (Fold (f, acc), q)
|
||||
|
||||
|
|
@ -677,6 +674,9 @@ let diff ?(cmp=Pervasives.compare) () q1 q2 =
|
|||
let fst q = map fst q
|
||||
let snd q = map snd q
|
||||
|
||||
let map1 f q = map (fun (x,y) -> f x, y) q
|
||||
let map2 f q = map (fun (x,y) -> x, f y) q
|
||||
|
||||
let flatten_opt q = filter_map _id q
|
||||
|
||||
let opt_get_exn q =
|
||||
|
|
|
|||
|
|
@ -162,16 +162,21 @@ val map_to_list : ('a,'b) Map.t t -> ('a*'b) list t
|
|||
(** {6 Aggregation} *)
|
||||
|
||||
val group_by : ?cmp:'b ord ->
|
||||
('a -> 'b) -> 'a collection t -> ('b,'a collection) Map.t t
|
||||
('a -> 'b) -> 'a collection t -> ('b,'a list) Map.t t
|
||||
(** [group_by f] takes a collection [c] as input, and returns
|
||||
a multimap [m] such that for each [x] in [c],
|
||||
[x] occurs in [m] under the key [f x]. In other words, [f] is used
|
||||
to obtain a key from [x], and [x] is added to the multimap using this key. *)
|
||||
|
||||
val group_by' : ?cmp:'b ord ->
|
||||
('a -> 'b) -> 'a collection t -> ('b * 'a list) collection t
|
||||
|
||||
val count : ?cmp:'a ord -> unit -> 'a collection t -> ('a, int) Map.t t
|
||||
(** [count c] returns a map from elements of [c] to the number
|
||||
of time those elements occur. *)
|
||||
|
||||
val count' : ?cmp:'a ord -> unit -> 'a collection t -> ('a * int) collection t
|
||||
|
||||
val fold : ('b -> 'a -> 'b) -> 'b -> 'a collection t -> 'b t
|
||||
(** Fold over the collection *)
|
||||
|
||||
|
|
@ -253,8 +258,13 @@ val diff : ?cmp:'a ord -> unit ->
|
|||
(** Specialized projection operators *)
|
||||
|
||||
val fst : ('a * 'b) collection t -> 'a collection t
|
||||
|
||||
val snd : ('a * 'b) collection t -> 'b collection t
|
||||
|
||||
val map1 : ('a -> 'b) -> ('a * 'c) collection t -> ('b * 'c) collection t
|
||||
|
||||
val map2 : ('a -> 'b) -> ('c * 'a) collection t -> ('c * 'b) collection t
|
||||
|
||||
val flatten_opt : 'a option collection t -> 'a collection t
|
||||
(** Flatten the collection by removing options *)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue