mirror of
https://github.com/c-cube/iter.git
synced 2025-12-06 11:15:32 -05:00
add filter_count
This commit is contained in:
parent
204b47cb3f
commit
6b5f4cf40e
3 changed files with 19 additions and 0 deletions
|
|
@ -207,6 +207,17 @@ let filter_mapi f seq k =
|
||||||
| None -> ()
|
| None -> ()
|
||||||
| Some y -> k y)
|
| 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 intersperse elem seq k =
|
||||||
let first = ref true in
|
let first = ref true in
|
||||||
seq (fun x -> (if !first then first := false else k elem); k x)
|
seq (fun x -> (if !first then first := false else k elem); k x)
|
||||||
|
|
|
||||||
|
|
@ -224,6 +224,10 @@ val filter_mapi : (int -> 'a -> 'b option) -> 'a t -> 'b t
|
||||||
(** Map with indices, and only keep non-[None] elements
|
(** Map with indices, and only keep non-[None] elements
|
||||||
@since 0.11 *)
|
@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
|
val intersperse : 'a -> 'a t -> 'a t
|
||||||
(** Insert the single element between every element of the sequence *)
|
(** Insert the single element between every element of the sequence *)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -197,6 +197,10 @@ val seq_list_map : f:('a -> 'b t) -> 'a list -> 'b list t
|
||||||
then calls {!seq_list}
|
then calls {!seq_list}
|
||||||
@since 0.11 *)
|
@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
|
val intersperse : x:'a -> 'a t -> 'a t
|
||||||
(** Insert the single element between every element of the sequence *)
|
(** Insert the single element between every element of the sequence *)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue