add CCKList.{zip, unzip}

This commit is contained in:
Simon Cruanes 2015-08-31 15:41:17 +02:00
parent 5a4d25b939
commit 40012fc84c
2 changed files with 30 additions and 0 deletions

View file

@ -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)

View file

@ -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.