diff --git a/src/Sequence.ml b/src/Sequence.ml index 9a05f36..1ed134b 100644 --- a/src/Sequence.ml +++ b/src/Sequence.ml @@ -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) diff --git a/src/Sequence.mli b/src/Sequence.mli index f9000b9..5a09e41 100644 --- a/src/Sequence.mli +++ b/src/Sequence.mli @@ -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 *) diff --git a/src/SequenceLabels.mli b/src/SequenceLabels.mli index a928522..31af7d1 100644 --- a/src/SequenceLabels.mli +++ b/src/SequenceLabels.mli @@ -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 *)