mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-15 23:36:01 -05:00
CCFun: put infix operators in Infix module
This commit is contained in:
parent
a5da43511b
commit
e50f11a196
2 changed files with 31 additions and 22 deletions
|
|
@ -3,11 +3,6 @@
|
||||||
|
|
||||||
(** {1 Basic Functions} *)
|
(** {1 Basic Functions} *)
|
||||||
|
|
||||||
(* default implem for some operators *)
|
|
||||||
|
|
||||||
let (|>) x f = f x
|
|
||||||
let (@@) f x = f x
|
|
||||||
|
|
||||||
let opaque_identity x = x
|
let opaque_identity x = x
|
||||||
|
|
||||||
(* import standard implementations, if any *)
|
(* 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 tap f x = ignore (f x); x
|
||||||
|
|
||||||
let (%>) = compose
|
|
||||||
|
|
||||||
let (%) f g x = f (g x)
|
|
||||||
|
|
||||||
let lexicographic f1 f2 x y =
|
let lexicographic f1 f2 x y =
|
||||||
let c = f1 x y in
|
let c = f1 x y in
|
||||||
if c <> 0 then c else f2 x y
|
if c <> 0 then c else f2 x y
|
||||||
|
|
@ -81,6 +72,17 @@ let rec iterate n f x =
|
||||||
(fun () -> iterate (-1) succ 10)
|
(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
|
module Monad(X : sig type t end) = struct
|
||||||
type 'a t = X.t -> 'a
|
type 'a t = X.t -> 'a
|
||||||
let return x _ = x
|
let return x _ = x
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,6 @@
|
||||||
|
|
||||||
include module type of CCShimsFun_
|
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
|
val compose : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c
|
||||||
(** [compose f g x] is [g (f x)]. Composition. *)
|
(** [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]].
|
[List.sort (compose_binop fst CCInt.compare) [1, true; 2, false; 1, false]].
|
||||||
@since 0.6*)
|
@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
|
val curry : ('a * 'b -> 'c) -> 'a -> 'b -> 'c
|
||||||
(** [curry f x y] is [f (x,y)].
|
(** [curry f x y] is [f (x,y)].
|
||||||
Convert a function which accepts a pair of arguments into a function which accepts two arguments. *)
|
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
|
val lexicographic : ('a -> 'a -> int) -> ('a -> 'a -> int) -> 'a -> 'a -> int
|
||||||
(** Lexicographic combination of comparison functions. *)
|
(** 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.
|
[x], [iterate 1 f x] is [f x], [iterate 2 f x] is [f (f x)], etc.
|
||||||
@since 2.1 *)
|
@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}
|
(** {2 Monad}
|
||||||
|
|
||||||
Functions with a fixed domain are monads in their codomain. *)
|
Functions with a fixed domain are monads in their codomain. *)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue