diff --git a/src/Sequence.ml b/src/Sequence.ml index dd6ea4c..af94eb8 100644 --- a/src/Sequence.ml +++ b/src/Sequence.ml @@ -446,14 +446,16 @@ let count (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 n = try Tbl.find tbl x with Not_found -> 0 in - Tbl.replace tbl x (n+1) - ); + let tbl = lazy ( + let tbl = Tbl.create 32 in + seq + (fun x -> + let n = try Tbl.find tbl x with Not_found -> 0 in + Tbl.replace tbl x (n+1)); + tbl + ) in fun yield -> - Tbl.iter (fun x n -> yield (x,n)) tbl + Tbl.iter (fun x n -> yield (x,n)) (Lazy.force tbl) (*$R [1;2;3;3;2;2;3;4] diff --git a/src/Sequence.mli b/src/Sequence.mli index 503b998..c7b19a2 100644 --- a/src/Sequence.mli +++ b/src/Sequence.mli @@ -276,6 +276,7 @@ val count : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) -> 'a t -> ('a * int) t (** Map each distinct element to its number of occurrences in the whole seq. Similar to [group_by seq |> map (fun l->List.hd l, List.length l)] + precondition: for any [x] and [y], if [eq x y] then [hash x=hash y] must hold. @since 0.10 *) val uniq : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t