diff --git a/core/CCPair.ml b/core/CCPair.ml index 136bc567..fa15d4c9 100644 --- a/core/CCPair.ml +++ b/core/CCPair.ml @@ -36,6 +36,11 @@ let map f g (x,y) = f x, g y let map_same f (x,y) = f x, f y +let map_fst f (x,_) = f x +let map_snd f (_,x) = f x + +let iter f (x,y) = f x y + let swap (x,y) = y, x let (<<<) = map1 @@ -47,6 +52,10 @@ let ( *** ) = map let ( &&& ) f g x = f x, g x let merge f (x,y) = f x y +let fold = merge + +let dup x = x,x +let dup_map f x = x, f x let equal f g (x1,y1) (x2,y2) = f x1 x2 && g y1 y2 diff --git a/core/CCPair.mli b/core/CCPair.mli index 45748dab..ba532242 100644 --- a/core/CCPair.mli +++ b/core/CCPair.mli @@ -36,6 +36,16 @@ val map : ('a -> 'c) -> ('b -> 'd) -> ('a * 'b) -> ('c * 'd) val map_same : ('a -> 'b) -> ('a*'a) -> ('b*'b) +val map_fst : ('a -> 'b) -> ('a * _) -> 'b +(** Compose the given function with [fst]. + @since NEXT_RELEASE *) + +val map_snd : ('a -> 'b) -> (_ * 'a) -> 'b +(** Compose the given function with [snd]. + @since NEXT_RELEASE *) + +val iter : ('a -> 'b -> unit) -> ('a * 'b) -> unit + val swap : ('a * 'b) -> ('b * 'a) (** Swap the components of the tuple *) @@ -55,6 +65,19 @@ val ( &&& ) : ('a -> 'b) -> ('a -> 'c) -> 'a -> ('b * 'c) val merge : ('a -> 'b -> 'c) -> ('a * 'b) -> 'c (** Uncurrying (merges the two components of a tuple) *) +val fold : ('a -> 'b -> 'c) -> ('a * 'b) -> 'c +(** Synonym to {!merge} + @since NEXT_RELEASE *) + +val dup : 'a -> ('a * 'a) +(** [dup x = (x,x)] (duplicate the value) + @since NEXT_RELEASE *) + +val dup_map : ('a -> 'b) -> 'a -> ('a * 'b) +(** [dup_map f x = (x, f x)]. Duplicates the value and applies the function + to the second copy. + @since NEXT_RELEASE *) + 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