From cd32539fea17fc39b69dd9ce024c8744b7ac128f Mon Sep 17 00:00:00 2001 From: Drup Date: Sat, 16 Jan 2016 14:45:55 +0100 Subject: [PATCH] Add map_by_2. --- sequence.ml | 10 ++++++++++ sequence.mli | 5 +++++ sequenceLabels.mli | 5 +++++ 3 files changed, 20 insertions(+) diff --git a/sequence.ml b/sequence.ml index c118fb9..547a4e5 100644 --- a/sequence.ml +++ b/sequence.ml @@ -70,6 +70,16 @@ let mapi f seq k = let i = ref 0 in seq (fun x -> k (f !i x); incr i) +let map_by_2 f seq k = + let r = ref None in + let f y = match !r with + | None -> r := Some y + | Some x -> k (f x y) + in + seq f ; + match !r with + | None -> () | Some x -> k x + let filter p seq k = seq (fun x -> if p x then k x) let append s1 s2 k = s1 k; s2 k diff --git a/sequence.mli b/sequence.mli index 593f477..4acfdd4 100644 --- a/sequence.mli +++ b/sequence.mli @@ -109,6 +109,11 @@ val map : ('a -> 'b) -> 'a t -> 'b t val mapi : (int -> 'a -> 'b) -> 'a t -> 'b t (** Map objects, along with their index in the sequence *) +val map_by_2 : ('a -> 'a -> 'a) -> 'a t -> 'a t + (** Map objects two by two. lazily. + The last element is kept in the sequence if the count is odd. + @since NEXT_RELEASE *) + val for_all : ('a -> bool) -> 'a t -> bool (** Do all elements satisfy the predicate? *) diff --git a/sequenceLabels.mli b/sequenceLabels.mli index 08f9a71..62894f4 100644 --- a/sequenceLabels.mli +++ b/sequenceLabels.mli @@ -87,6 +87,11 @@ val map : f:('a -> 'b) -> 'a t -> 'b t val mapi : f:(int -> 'a -> 'b) -> 'a t -> 'b t (** Map objects, along with their index in the sequence *) +val map_by_2 : f:('a -> 'a -> 'a) -> 'a t -> 'a t + (** Map objects two by two. lazily. + The last element is kept in the sequence if the count is odd. + @since NEXT_RELEASE *) + val for_all : f:('a -> bool) -> 'a t -> bool (** Do all elements satisfy the predicate? *)