add count

This commit is contained in:
Simon Cruanes 2017-02-02 21:27:46 +01:00
parent fa26fa0f27
commit 12743ab24f
3 changed files with 34 additions and 0 deletions

View file

@ -411,6 +411,28 @@ let group_by (type k) ?(hash=Hashtbl.hash) ?(eq=(=)) seq =
|> OUnit.assert_equal [[1];[2;2;2];[3;3;3];[4]]
*)
let count (type k) ?(hash=Hashtbl.hash) ?(eq=(=)) seq =
let module Tbl = Hashtbl.Make(struct
type t = k
let equal = eq
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)
);
fun yield ->
Tbl.iter (fun x n -> yield (x,n)) tbl
(*$R
[1;2;3;3;2;2;3;4]
|> of_list |> count ?eq:None ?hash:None |> sort ?cmp:None |> to_list
|> OUnit.assert_equal [1,1;2,3;3,3;4,1]
*)
let uniq ?(eq=fun x y -> x = y) seq k =
let has_prev = ref false
and prev = ref (Obj.magic 0) in (* avoid option type, costly *)

View file

@ -241,6 +241,12 @@ val group_by : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) ->
The result sequence is traversable as many times as required.
@since 0.6 *)
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)]
@since NEXT_RELEASE *)
val uniq : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t
(** Remove consecutive duplicate elements. Basically this is
like [fun seq -> map List.hd (group seq)]. *)

View file

@ -212,6 +212,12 @@ val group_by : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) ->
The result sequence is traversable as many times as required.
@since 0.6 *)
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)]
@since NEXT_RELEASE *)
val uniq : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t
(** Remove consecutive duplicate elements. Basically this is
like [fun seq -> map List.hd (group seq)]. *)