diff --git a/src/core/CCPair.ml b/src/core/CCPair.ml index 875d4167..1e24a082 100644 --- a/src/core/CCPair.ml +++ b/src/core/CCPair.ml @@ -2,27 +2,53 @@ (** {1 Tuple Functions} *) +[@@@ifge 5.4] + +include Pair + +[@@@else_] + type ('a, 'b) t = 'a * 'b let make x y = x, y +let fst = fst +let snd = snd +let swap (x, y) = y, x let map_fst f (x, y) = f x, y let map_snd f (x, y) = x, f y let map f g (x, y) = f x, g y + +[@@@endif] + let map_same f (x, y) = f x, f y let map2 f g (a, b) (x, y) = f a x, g b y let map_same2 f (a, b) (x, y) = f a x, f b y let fst_map f (x, _) = f x let snd_map f (_, x) = f x + +[@@@iflt 5.4] + let iter f (x, y) = f x y -let swap (x, y) = y, x + +[@@@endif] + let ( <<< ) = map_fst let ( >>> ) = map_snd let ( *** ) = map let ( &&& ) f g x = f x, g x let merge f (x, y) = f x y + +[@@@iflt 5.4] + let fold = merge + +[@@@endif] + let dup x = x, x let dup_map f x = x, f x + +[@@@iflt 5.4] + let equal f g (x1, y1) (x2, y2) = f x1 x2 && g y1 y2 let compare f g (x1, y1) (x2, y2) = @@ -32,6 +58,8 @@ let compare f g (x1, y1) (x2, y2) = else g y1 y2 +[@@@endif] + let to_string ?(sep = ", ") a_to_string b_to_string (x, y) = Printf.sprintf "%s%s%s" (a_to_string x) sep (b_to_string y) diff --git a/src/core/CCPair.mli b/src/core/CCPair.mli index 2492bbde..76cd1af0 100644 --- a/src/core/CCPair.mli +++ b/src/core/CCPair.mli @@ -2,12 +2,27 @@ (** Tuple Functions *) +[@@@ifge 5.4] + +include module type of Pair + +[@@@else_] + type ('a, 'b) t = 'a * 'b val make : 'a -> 'b -> ('a, 'b) t (** Make a tuple from its components. @since 0.16 *) +val fst : 'a * 'b -> 'a +(** [fst (a, b)] returns [a] *) + +val snd : 'a * 'b -> 'b +(** [snd (a, b)] returns [b] *) + +val swap : 'a * 'b -> 'b * 'a +(** Swap the components of the tuple. *) + val map_fst : ('a -> 'b) -> 'a * 'c -> 'b * 'c (** [map_fst f (x, y)] returns [(f x, y)]. Renamed from [map1] since 3.0. *) @@ -19,6 +34,8 @@ val map_snd : ('a -> 'b) -> 'c * 'a -> 'c * 'b val map : ('a -> 'c) -> ('b -> 'd) -> 'a * 'b -> 'c * 'd (** Synonym to {!( *** )}. Map on both sides of a tuple. *) +[@@@endif] + val map_same : ('a -> 'b) -> 'a * 'a -> 'b * 'b (** Like {!map} but specialized for pairs with elements of the same type. *) @@ -45,10 +62,11 @@ val snd_map : ('a -> 'b) -> _ * 'a -> 'b Rename from [map_snd] since 3.0. @since 0.3.3 *) +[@@@iflt 5.4] + val iter : ('a -> 'b -> unit) -> 'a * 'b -> unit -val swap : 'a * 'b -> 'b * 'a -(** Swap the components of the tuple. *) +[@@@endif] val ( <<< ) : ('a -> 'b) -> 'a * 'c -> 'b * 'c (** Map on the left side of the tuple. *) @@ -66,10 +84,14 @@ val ( &&& ) : ('a -> 'b) -> ('a -> 'c) -> 'a -> 'b * 'c val merge : ('a -> 'b -> 'c) -> 'a * 'b -> 'c (** Uncurrying (merges the two components of a tuple). *) +[@@@iflt 5.4] + val fold : ('a -> 'b -> 'c) -> 'a * 'b -> 'c (** Synonym to {!merge}. @since 0.3.3 *) +[@@@endif] + val dup : 'a -> 'a * 'a (** [dup x = (x,x)] (duplicate the value). @since 0.3.3 *) @@ -79,12 +101,16 @@ val dup_map : ('a -> 'b) -> 'a -> 'a * 'b to the second copy. @since 0.3.3 *) +[@@@iflt 5.4] + val equal : ('a -> 'a -> bool) -> ('b -> 'b -> bool) -> 'a * 'b -> 'a * 'b -> bool val compare : ('a -> 'a -> int) -> ('b -> 'b -> int) -> 'a * 'b -> 'a * 'b -> int +[@@@endif] + val to_string : ?sep:string -> ('a -> string) -> ('b -> string) -> 'a * 'b -> string (** Print tuple in a string