diff --git a/src/Sequence.ml b/src/Sequence.ml index a9c0f96..9397ae1 100644 --- a/src/Sequence.ml +++ b/src/Sequence.ml @@ -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 diff --git a/src/Sequence.mli b/src/Sequence.mli index 2d686da..d08af7b 100644 --- a/src/Sequence.mli +++ b/src/Sequence.mli @@ -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 *) diff --git a/src/sequenceLabels.mli b/src/sequenceLabels.mli index 940c28d..d764c69 100644 --- a/src/sequenceLabels.mli +++ b/src/sequenceLabels.mli @@ -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.