diff --git a/src/iter/CCKList.ml b/src/iter/CCKList.ml index 0b7de890..adf6421e 100644 --- a/src/iter/CCKList.ml +++ b/src/iter/CCKList.ml @@ -303,6 +303,26 @@ let rec merge cmp l1 l2 () = match l1(), l2() with then `Cons (x1, merge cmp l1' l2) else `Cons (x2, merge cmp l1 l2') +let rec zip a b () = match a(), b() with + | `Nil, _ + | _, `Nil -> `Nil + | `Cons (x, a'), `Cons (y, b') -> `Cons ((x,y), zip a' b') + +let unzip l = + let rec first l () = match l() with + | `Nil -> `Nil + | `Cons ((x,_), tl) -> `Cons (x, first tl) + and second l () = match l() with + | `Nil -> `Nil + | `Cons ((_, y), tl) -> `Cons (y, second tl) + in + first l, second l + +(*$Q + Q.(list (pair int int)) (fun l -> \ + let l = CCKList.of_list l in let a, b = unzip l in equal (=) l (zip a b)) +*) + (** {2 Implementations} *) let return x () = `Cons (x, nil) diff --git a/src/iter/CCKList.mli b/src/iter/CCKList.mli index 76c94bdc..268fd39c 100644 --- a/src/iter/CCKList.mli +++ b/src/iter/CCKList.mli @@ -171,6 +171,16 @@ val exists2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool val merge : 'a ord -> 'a t -> 'a t -> 'a t (** Merge two sorted iterators into a sorted iterator *) +val zip : 'a t -> 'b t -> ('a * 'b) t +(** Combine elements pairwise. Stops as soon as one of the lists stops. + @since NEXT_RELEASE *) + +val unzip : ('a * 'b) t -> 'a t * 'b t +(** Splits each tuple in the list + @since NEXT_RELEASE *) + +(** {2 Misc} *) + val sort : ?cmp:'a ord -> 'a t -> 'a t (** Eager sort. Requires the iterator to be finite. O(n ln(n)) time and space.