CCFun: put infix operators in Infix module

This commit is contained in:
Kye W. Shi 2020-06-19 14:53:25 -07:00 committed by Simon Cruanes
parent a5da43511b
commit a03b6e68e3
2 changed files with 31 additions and 22 deletions

View file

@ -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

View file

@ -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. *)