make group_by lazy

This commit is contained in:
Simon Cruanes 2017-12-15 12:45:27 +01:00
parent ab4a22f8a7
commit 6be7dddee2

View file

@ -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 = lazy (
let tbl = Tbl.create 32 in let tbl = Tbl.create 32 in
seq seq
(fun x -> (fun x ->
let l = try Tbl.find tbl x with Not_found -> [] in let l = try Tbl.find tbl x with Not_found -> [] in
Tbl.replace tbl x (x::l) 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]