Add map_by_2.

This commit is contained in:
Drup 2016-01-16 14:45:55 +01:00
parent 7bc7407ed2
commit cd32539fea
3 changed files with 20 additions and 0 deletions

View file

@ -70,6 +70,16 @@ let mapi f seq k =
let i = ref 0 in let i = ref 0 in
seq (fun x -> k (f !i x); incr i) 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 filter p seq k = seq (fun x -> if p x then k x)
let append s1 s2 k = s1 k; s2 k let append s1 s2 k = s1 k; s2 k

View file

@ -109,6 +109,11 @@ val map : ('a -> 'b) -> 'a t -> 'b t
val mapi : (int -> 'a -> 'b) -> 'a t -> 'b t val mapi : (int -> 'a -> 'b) -> 'a t -> 'b t
(** Map objects, along with their index in the sequence *) (** 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 val for_all : ('a -> bool) -> 'a t -> bool
(** Do all elements satisfy the predicate? *) (** Do all elements satisfy the predicate? *)

View file

@ -87,6 +87,11 @@ val map : f:('a -> 'b) -> 'a t -> 'b t
val mapi : f:(int -> '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 *) (** 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 val for_all : f:('a -> bool) -> 'a t -> bool
(** Do all elements satisfy the predicate? *) (** Do all elements satisfy the predicate? *)