CCPair(chore): sync CCPair with Stdlib.Pair

This commit is contained in:
Emmanuel Arrighi 2026-02-06 12:34:00 +01:00
parent bb31265e52
commit 3af76f266c
2 changed files with 57 additions and 3 deletions

View file

@ -2,27 +2,53 @@
(** {1 Tuple Functions} *) (** {1 Tuple Functions} *)
[@@@ifge 5.4]
include Pair
[@@@else_]
type ('a, 'b) t = 'a * 'b type ('a, 'b) t = 'a * 'b
let make x y = x, y 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_fst f (x, y) = f x, y
let map_snd f (x, y) = x, f y let map_snd f (x, y) = x, f y
let map f g (x, y) = f x, g y let map f g (x, y) = f x, g y
[@@@endif]
let map_same f (x, y) = f x, f y 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 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 map_same2 f (a, b) (x, y) = f a x, f b y
let fst_map f (x, _) = f x let fst_map f (x, _) = f x
let snd_map f (_, x) = f x let snd_map f (_, x) = f x
[@@@iflt 5.4]
let iter f (x, y) = f x y let iter f (x, y) = f x y
let swap (x, y) = y, x
[@@@endif]
let ( <<< ) = map_fst let ( <<< ) = map_fst
let ( >>> ) = map_snd let ( >>> ) = map_snd
let ( *** ) = map let ( *** ) = map
let ( &&& ) f g x = f x, g x let ( &&& ) f g x = f x, g x
let merge f (x, y) = f x y let merge f (x, y) = f x y
[@@@iflt 5.4]
let fold = merge let fold = merge
[@@@endif]
let dup x = x, x let dup x = x, x
let dup_map f x = x, f 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 equal f g (x1, y1) (x2, y2) = f x1 x2 && g y1 y2
let compare f g (x1, y1) (x2, y2) = let compare f g (x1, y1) (x2, y2) =
@ -32,6 +58,8 @@ let compare f g (x1, y1) (x2, y2) =
else else
g y1 y2 g y1 y2
[@@@endif]
let to_string ?(sep = ", ") a_to_string b_to_string (x, y) = 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) Printf.sprintf "%s%s%s" (a_to_string x) sep (b_to_string y)

View file

@ -2,12 +2,27 @@
(** Tuple Functions *) (** Tuple Functions *)
[@@@ifge 5.4]
include module type of Pair
[@@@else_]
type ('a, 'b) t = 'a * 'b type ('a, 'b) t = 'a * 'b
val make : 'a -> 'b -> ('a, 'b) t val make : 'a -> 'b -> ('a, 'b) t
(** Make a tuple from its components. (** Make a tuple from its components.
@since 0.16 *) @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 val map_fst : ('a -> 'b) -> 'a * 'c -> 'b * 'c
(** [map_fst f (x, y)] returns [(f x, y)]. (** [map_fst f (x, y)] returns [(f x, y)].
Renamed from [map1] since 3.0. *) 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 val map : ('a -> 'c) -> ('b -> 'd) -> 'a * 'b -> 'c * 'd
(** Synonym to {!( *** )}. Map on both sides of a tuple. *) (** Synonym to {!( *** )}. Map on both sides of a tuple. *)
[@@@endif]
val map_same : ('a -> 'b) -> 'a * 'a -> 'b * 'b val map_same : ('a -> 'b) -> 'a * 'a -> 'b * 'b
(** Like {!map} but specialized for pairs with elements of the same type. *) (** 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. Rename from [map_snd] since 3.0.
@since 0.3.3 *) @since 0.3.3 *)
[@@@iflt 5.4]
val iter : ('a -> 'b -> unit) -> 'a * 'b -> unit val iter : ('a -> 'b -> unit) -> 'a * 'b -> unit
val swap : 'a * 'b -> 'b * 'a [@@@endif]
(** Swap the components of the tuple. *)
val ( <<< ) : ('a -> 'b) -> 'a * 'c -> 'b * 'c val ( <<< ) : ('a -> 'b) -> 'a * 'c -> 'b * 'c
(** Map on the left side of the tuple. *) (** 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 val merge : ('a -> 'b -> 'c) -> 'a * 'b -> 'c
(** Uncurrying (merges the two components of a tuple). *) (** Uncurrying (merges the two components of a tuple). *)
[@@@iflt 5.4]
val fold : ('a -> 'b -> 'c) -> 'a * 'b -> 'c val fold : ('a -> 'b -> 'c) -> 'a * 'b -> 'c
(** Synonym to {!merge}. (** Synonym to {!merge}.
@since 0.3.3 *) @since 0.3.3 *)
[@@@endif]
val dup : 'a -> 'a * 'a val dup : 'a -> 'a * 'a
(** [dup x = (x,x)] (duplicate the value). (** [dup x = (x,x)] (duplicate the value).
@since 0.3.3 *) @since 0.3.3 *)
@ -79,12 +101,16 @@ val dup_map : ('a -> 'b) -> 'a -> 'a * 'b
to the second copy. to the second copy.
@since 0.3.3 *) @since 0.3.3 *)
[@@@iflt 5.4]
val equal : val equal :
('a -> 'a -> bool) -> ('b -> 'b -> bool) -> 'a * 'b -> 'a * 'b -> bool ('a -> 'a -> bool) -> ('b -> 'b -> bool) -> 'a * 'b -> 'a * 'b -> bool
val compare : val compare :
('a -> 'a -> int) -> ('b -> 'b -> int) -> 'a * 'b -> 'a * 'b -> int ('a -> 'a -> int) -> ('b -> 'b -> int) -> 'a * 'b -> 'a * 'b -> int
[@@@endif]
val to_string : val to_string :
?sep:string -> ('a -> string) -> ('b -> string) -> 'a * 'b -> string ?sep:string -> ('a -> string) -> ('b -> string) -> 'a * 'b -> string
(** Print tuple in a string (** Print tuple in a string