From 2aa8416b1c40fd8600a9dfb8c438506a2072d20f Mon Sep 17 00:00:00 2001 From: Fardale Date: Wed, 25 Dec 2024 14:27:47 +0100 Subject: [PATCH] CCFun(cleanup): align CCFun.compose with the stdlib Conditionally define CCFun.compose and align its definition with the stdlib. The arguments are now swapped. --- CHANGELOG.md | 5 +++-- src/core/CCFun.ml | 10 ++++++++-- src/core/CCFun.mli | 12 +++++++++--- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cc19e0d7..a77bd2d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,9 @@ # Changelog ## main -- change the semantic of CCFloat.{min,max} with respect to NaN to follow the Stdlib -- change the semantic of CCInt.rem with respect to negative number to follow the Stdlib +- breaking: invert the argument of CCFun.compose to align it with the Stdlib +- breaking: change the semantic of CCFloat.{min,max} with respect to NaN to follow the Stdlib +- breaking: change the semantic of CCInt.rem with respect to negative number to follow the Stdlib ## 3.15 diff --git a/src/core/CCFun.ml b/src/core/CCFun.ml index abb2a6a2..7605d766 100644 --- a/src/core/CCFun.ml +++ b/src/core/CCFun.ml @@ -10,7 +10,13 @@ include Fun let[@inline] and_pred f g x = f x && g x let[@inline] or_pred f g x = f x || g x -let[@inline] compose f g x = g (f x) + +[@@@iflt 5.2] + +let[@inline] compose f g x = f (g x) + +[@@@endif] + let[@inline] compose_binop f g x y = g (f x) (f y) let[@inline] curry f x y = f (x, y) let[@inline] uncurry f (x, y) = f x y @@ -63,7 +69,7 @@ let rec iterate n f x = module Infix = struct (* default implem for some operators *) - let ( %> ) = compose + let ( %> ) f g = compose g f let[@inline] ( % ) f g x = f (g x) let ( let@ ) = ( @@ ) let ( ||> ) (a, b) f = f a b diff --git a/src/core/CCFun.mli b/src/core/CCFun.mli index 88261101..d5d33c74 100644 --- a/src/core/CCFun.mli +++ b/src/core/CCFun.mli @@ -17,8 +17,13 @@ val or_pred : ('a -> bool) -> ('a -> bool) -> 'a -> bool @since 3.13.1 *) -val compose : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c -(** [compose f g x] is [g (f x)]. Composition. *) +[@@@iflt 5.2] + +val compose : ('b -> 'c) -> ('a -> 'b) -> 'a -> 'c +(** [compose f g x] is [f (g x)]. Composition. + @since NEXT_RELEASE arguments are inversted *) + +[@@@endif] val compose_binop : ('a -> 'b) -> ('b -> 'b -> 'c) -> 'a -> 'a -> 'c (** [compose_binop f g] is [fun x y -> g (f x) (f y)]. @@ -84,7 +89,8 @@ val iterate : int -> ('a -> 'a) -> 'a -> 'a module Infix : sig val ( %> ) : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c - (** [(f %> g) x] or [(%>) f g x] is [g (f x)]. Alias to [compose]. *) + (** [(f %> g) x] or [(%>) f g x] is [g (f x)]. Infix version of [compose]. + The order of the arguments of [%>] and {!compose} are inverted. *) val ( % ) : ('b -> 'c) -> ('a -> 'b) -> 'a -> 'c (** [(f % g) x] or [(%) f g x] is [f (g x)]. Mathematical composition. *)