From d9ccb619a1b260f533361b235c40d7163e120a23 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Tue, 20 May 2014 17:30:17 +0200 Subject: [PATCH] additional utils for pairs and functions --- core/CCFun.ml | 6 ++++++ core/CCFun.mli | 7 +++++++ core/CCPair.ml | 4 ++++ core/CCPair.mli | 11 +++++++++++ 4 files changed, 28 insertions(+) diff --git a/core/CCFun.ml b/core/CCFun.ml index 2a675b42..849209bb 100644 --- a/core/CCFun.ml +++ b/core/CCFun.ml @@ -30,6 +30,12 @@ let (|>) x f = f x let compose f g x = g (f x) +let flip f x y = f y x + +let curry f x y = f (x,y) + +let uncurry f (x,y) = f x y + let (%>) = compose let (%) f g x = f (g x) diff --git a/core/CCFun.mli b/core/CCFun.mli index dc3085a5..84381d62 100644 --- a/core/CCFun.mli +++ b/core/CCFun.mli @@ -35,6 +35,13 @@ val compose : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c val (%>) : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c (** Alias to [compose] *) +val flip : ('a -> 'b -> 'c) -> 'b -> 'a -> 'c +(** flip arguments *) + +val curry : ('a * 'b -> 'c) -> 'a -> 'b -> 'c + +val uncurry : ('a -> 'b -> 'c) -> ('a * 'b) -> 'c + val (%) : ('b -> 'c) -> ('a -> 'b) -> 'a -> 'c (** Mathematical composition *) diff --git a/core/CCPair.ml b/core/CCPair.ml index ec00f2cd..136bc567 100644 --- a/core/CCPair.ml +++ b/core/CCPair.ml @@ -44,6 +44,10 @@ let (>>>) = map2 let ( *** ) = map +let ( &&& ) f g x = f x, g x + +let merge f (x,y) = f x y + let equal f g (x1,y1) (x2,y2) = f x1 x2 && g y1 y2 let compare f g (x1,y1) (x2,y2) = diff --git a/core/CCPair.mli b/core/CCPair.mli index 4c76413f..45748dab 100644 --- a/core/CCPair.mli +++ b/core/CCPair.mli @@ -37,12 +37,23 @@ val map : ('a -> 'c) -> ('b -> 'd) -> ('a * 'b) -> ('c * 'd) val map_same : ('a -> 'b) -> ('a*'a) -> ('b*'b) val swap : ('a * 'b) -> ('b * 'a) +(** Swap the components of the tuple *) val (<<<) : ('a -> 'b) -> ('a * 'c) -> ('b * 'c) +(** Map on the left side of the tuple *) val (>>>) : ('a -> 'b) -> ('c * 'a) -> ('c * 'b) +(** Map on the right side of the tuple *) val ( *** ) : ('a -> 'c) -> ('b -> 'd) -> ('a * 'b) -> ('c * 'd) +(** Map on both sides of a tuple *) + +val ( &&& ) : ('a -> 'b) -> ('a -> 'c) -> 'a -> ('b * 'c) +(** [f &&& g] is [fun x -> f x, g x]. It splits the computations into + two parts *) + +val merge : ('a -> 'b -> 'c) -> ('a * 'b) -> 'c +(** Uncurrying (merges the two components of a tuple) *) val equal : ('a -> 'a -> bool) -> ('b -> 'b -> bool) -> ('a * 'b) -> ('a * 'b) -> bool