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
|
||||
end) in
|
||||
(* compute group table *)
|
||||
let tbl = Tbl.create 32 in
|
||||
seq
|
||||
(fun x ->
|
||||
let l = try Tbl.find tbl x with Not_found -> [] in
|
||||
Tbl.replace tbl x (x::l)
|
||||
);
|
||||
let tbl = lazy (
|
||||
let tbl = Tbl.create 32 in
|
||||
seq
|
||||
(fun x ->
|
||||
let l = try Tbl.find tbl x with Not_found -> [] in
|
||||
Tbl.replace tbl x (x::l));
|
||||
tbl
|
||||
) in
|
||||
fun yield ->
|
||||
Tbl.iter (fun _ l -> yield l) tbl
|
||||
Tbl.iter (fun _ l -> yield l) (Lazy.force tbl)
|
||||
|
||||
(*$R
|
||||
[1;2;3;3;2;2;3;4]
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue