From a03b6e68e3a278484fcc4505946161f7cbfe4187 Mon Sep 17 00:00:00 2001 From: "Kye W. Shi" Date: Fri, 19 Jun 2020 14:53:25 -0700 Subject: [PATCH] CCFun: put infix operators in Infix module --- src/core/CCFun.ml | 20 +++++++++++--------- src/core/CCFun.mli | 33 ++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/core/CCFun.ml b/src/core/CCFun.ml index df7230e4..6208fb01 100644 --- a/src/core/CCFun.ml +++ b/src/core/CCFun.ml @@ -3,11 +3,6 @@ (** {1 Basic Functions} *) -(* default implem for some operators *) - -let (|>) x f = f x -let (@@) f x = f x - let opaque_identity x = x (* import standard implementations, if any *) @@ -26,10 +21,6 @@ let uncurry f (x,y) = f x y let tap f x = ignore (f x); x -let (%>) = compose - -let (%) f g x = f (g x) - let lexicographic f1 f2 x y = let c = f1 x y in if c <> 0 then c else f2 x y @@ -81,6 +72,17 @@ let rec iterate n f x = (fun () -> iterate (-1) succ 10) *) +module Infix = struct + (* default implem for some operators *) + let (|>) x f = f x + let (@@) f x = f x + + let (%>) = compose + let (%) f g x = f (g x) +end + +include Infix + module Monad(X : sig type t end) = struct type 'a t = X.t -> 'a let return x _ = x diff --git a/src/core/CCFun.mli b/src/core/CCFun.mli index 9a1f5fa8..64814f31 100644 --- a/src/core/CCFun.mli +++ b/src/core/CCFun.mli @@ -5,9 +5,6 @@ include module type of CCShimsFun_ -val (|>) : 'a -> ('a -> 'b) -> 'b -(** [x |> f] is the same as [f x]. A 'pipe' operator. *) - val compose : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c (** [compose f g x] is [g (f x)]. Composition. *) @@ -17,13 +14,6 @@ val compose_binop : ('a -> 'b) -> ('b -> 'b -> 'c) -> 'a -> 'a -> 'c [List.sort (compose_binop fst CCInt.compare) [1, true; 2, false; 1, false]]. @since 0.6*) -val (%>) : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c -(** [(f %> g) x] or [(%>) f g x] is [g (f x)]. Alias to [compose]. *) - -val (@@) : ('a -> 'b) -> 'a -> 'b -(** [f @@ x] is the same as [f x], but right-associative. - @since 0.5 *) - val curry : ('a * 'b -> 'c) -> 'a -> 'b -> 'c (** [curry f x y] is [f (x,y)]. Convert a function which accepts a pair of arguments into a function which accepts two arguments. *) @@ -41,9 +31,6 @@ val tap : ('a -> _) -> 'a -> 'a ]} *) -val (%) : ('b -> 'c) -> ('a -> 'b) -> 'a -> 'c -(** [(f % g) x] or [(%) f g x] is [f (g x)]. Mathematical composition. *) - val lexicographic : ('a -> 'a -> int) -> ('a -> 'a -> int) -> 'a -> 'a -> int (** Lexicographic combination of comparison functions. *) @@ -79,6 +66,26 @@ val iterate : int -> ('a -> 'a) -> 'a -> 'a [x], [iterate 1 f x] is [f x], [iterate 2 f x] is [f (f x)], etc. @since 2.1 *) +(** {2 Infix} + + Infix operators. *) + +module Infix : sig + + val (|>) : 'a -> ('a -> 'b) -> 'b + (** [x |> f] is the same as [f x]. A 'pipe' operator. *) + + val (@@) : ('a -> 'b) -> 'a -> 'b + (** [f @@ x] is the same as [f x], but right-associative. + @since 0.5 *) + + val (%>) : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c + (** [(f %> g) x] or [(%>) f g x] is [g (f x)]. Alias to [compose]. *) + + val (%) : ('b -> 'c) -> ('a -> 'b) -> 'a -> 'c + (** [(f % g) x] or [(%) f g x] is [f (g x)]. Mathematical composition. *) +end + (** {2 Monad} Functions with a fixed domain are monads in their codomain. *)