From 4aed639cc79bb1332ce800c8f7d8cda3ee792917 Mon Sep 17 00:00:00 2001 From: Hongchang Wu Date: Sat, 8 Jan 2022 21:23:11 -0500 Subject: [PATCH] Add CCSeq.zip_with --- src/core/CCSeq.ml | 12 ++++++++++++ src/core/CCSeq.mli | 6 ++++++ 2 files changed, 18 insertions(+) diff --git a/src/core/CCSeq.ml b/src/core/CCSeq.ml index 55a32593..c1e5b435 100644 --- a/src/core/CCSeq.ml +++ b/src/core/CCSeq.ml @@ -341,6 +341,18 @@ let rec zip a b () = match a(), b() with | _, Nil -> Nil | Cons (x, a'), Cons (y, b') -> Cons ((x,y), zip a' b') +let rec zip_with f a b () = match a(), b() with + | Nil, _ + | _, Nil -> Nil + | Cons (x, a'), Cons (y, b') -> Cons (f x y, zip_with f a' b') + +(*$Q + Q.(pair (list int) (list int)) (fun (l1, l2) -> \ + let l1 = of_list l1 in \ + let l2 = of_list l2 in \ + zip l1 l2 |> to_list = (zip_with (fun x y -> (x, y)) l1 l2 |> to_list)) +*) + let unzip l = let rec first l () = match l() with | Nil -> Nil diff --git a/src/core/CCSeq.mli b/src/core/CCSeq.mli index 1719ba92..bb7ddf82 100644 --- a/src/core/CCSeq.mli +++ b/src/core/CCSeq.mli @@ -178,6 +178,12 @@ val zip : 'a t -> 'b t -> ('a * 'b) t (** Combine elements pairwise. Stop as soon as one of the lists stops. @since 0.13 *) +val zip_with : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t +(** Generalization of {!zip}. Combine elements with the function given as the first + argument. Stop as soon as one of the lists stops. + + @since NEXT_RELEASE *) + val unzip : ('a * 'b) t -> 'a t * 'b t (** Split each tuple in the list. @since 0.13 *)