mirror of
https://github.com/c-cube/iter.git
synced 2025-12-06 11:15:32 -05:00
make group_by lazy
This commit is contained in:
parent
ab4a22f8a7
commit
6be7dddee2
1 changed files with 9 additions and 7 deletions
|
|
@ -422,14 +422,16 @@ let group_by (type k) ?(hash=Hashtbl.hash) ?(eq=(=)) seq =
|
||||||
let hash = hash
|
let hash = hash
|
||||||
end) in
|
end) in
|
||||||
(* compute group table *)
|
(* compute group table *)
|
||||||
let tbl = Tbl.create 32 in
|
let tbl = lazy (
|
||||||
seq
|
let tbl = Tbl.create 32 in
|
||||||
(fun x ->
|
seq
|
||||||
let l = try Tbl.find tbl x with Not_found -> [] in
|
(fun x ->
|
||||||
Tbl.replace tbl x (x::l)
|
let l = try Tbl.find tbl x with Not_found -> [] in
|
||||||
);
|
Tbl.replace tbl x (x::l));
|
||||||
|
tbl
|
||||||
|
) in
|
||||||
fun yield ->
|
fun yield ->
|
||||||
Tbl.iter (fun _ l -> yield l) tbl
|
Tbl.iter (fun _ l -> yield l) (Lazy.force tbl)
|
||||||
|
|
||||||
(*$R
|
(*$R
|
||||||
[1;2;3;3;2;2;3;4]
|
[1;2;3;3;2;2;3;4]
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue