add filter_count

This commit is contained in:
Simon Cruanes 2018-01-14 16:18:18 -06:00
parent 204b47cb3f
commit 6b5f4cf40e
3 changed files with 19 additions and 0 deletions

View file

@ -207,6 +207,17 @@ let filter_mapi f seq k =
| None -> ()
| Some y -> k y)
let filter_count f seq =
let i = ref 0 in
seq (fun x -> if f x then incr i);
!i
(*$Q
Q.(list int) (fun l -> \
let seq = of_list l and f x = x mod 2 = 0 in \
filter_count f seq = (filter f seq |> length))
*)
let intersperse elem seq k =
let first = ref true in
seq (fun x -> (if !first then first := false else k elem); k x)

View file

@ -224,6 +224,10 @@ val filter_mapi : (int -> 'a -> 'b option) -> 'a t -> 'b t
(** Map with indices, and only keep non-[None] elements
@since 0.11 *)
val filter_count : ('a -> bool) -> 'a t -> int
(** Count how many elements satisfy the given predicate
@since NEXT_RELEASE *)
val intersperse : 'a -> 'a t -> 'a t
(** Insert the single element between every element of the sequence *)

View file

@ -197,6 +197,10 @@ val seq_list_map : f:('a -> 'b t) -> 'a list -> 'b list t
then calls {!seq_list}
@since 0.11 *)
val filter_count : f:('a -> bool) -> 'a t -> int
(** Count how many elements satisfy the given predicate
@since NEXT_RELEASE *)
val intersperse : x:'a -> 'a t -> 'a t
(** Insert the single element between every element of the sequence *)