add Sequence.filter_mapi

This commit is contained in:
Simon Cruanes 2017-09-14 09:18:02 +02:00
parent dee62387aa
commit d59d96a513
3 changed files with 18 additions and 2 deletions

View file

@ -197,8 +197,16 @@ let seq_list l = seq_list_map (fun x->x) l
let filter_map f seq k =
seq (fun x -> match f x with
| None -> ()
| Some y -> k y
)
| Some y -> k y)
let filter_mapi f seq k =
let i = ref 0 in
seq (fun x ->
let j = !i in
incr i;
match f j x with
| None -> ()
| Some y -> k y)
let intersperse elem seq k =
let first = ref true in

View file

@ -215,6 +215,10 @@ val filter_map : ('a -> 'b option) -> 'a t -> 'b t
Formerly [fmap]
@since 0.5 *)
val filter_mapi : (int -> 'a -> 'b option) -> 'a t -> 'b t
(** Map with indices, and only keep non-[None] elements
@since NEXT_RELEASE *)
val intersperse : 'a -> 'a t -> 'a t
(** Insert the single element between every element of the sequence *)

View file

@ -176,6 +176,10 @@ val flat_map_l : f:('a -> 'b list) -> 'a t -> 'b t
val filter_map : f:('a -> 'b option) -> 'a t -> 'b t
(** Alias to {!fmap} with a more explicit name *)
val filter_mapi : f:(int -> 'a -> 'b option) -> 'a t -> 'b t
(** Map with indices, and only keep non-[None] elements
@since NEXT_RELEASE *)
val seq_list : 'a t list -> 'a list t
(** [seq_list l] returns all the ways to pick one element in each sub-sequence
in [l]. Assumes the sub-sequences can be iterated on several times.