From b2d9e690420bcb472bf714d09163e5f649a640f5 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Tue, 10 Dec 2019 21:45:49 -0600 Subject: [PATCH] feat: put the let operators inside the Infix modules when relevant --- src/core/CCArray.ml | 27 ++++++++++++--------------- src/core/CCArray.mli | 4 ++++ src/core/CCArrayLabels.mli | 4 ++++ src/core/CCList.ml | 25 +++++++++++-------------- src/core/CCList.mli | 7 +++++-- src/core/CCListLabels.mli | 7 +++++-- src/core/CCOpt.ml | 27 +++++++++++++-------------- src/core/CCOpt.mli | 13 +++++++++---- src/core/CCResult.ml | 29 ++++++++++++++--------------- src/core/CCResult.mli | 13 +++++++++---- 10 files changed, 86 insertions(+), 70 deletions(-) diff --git a/src/core/CCArray.ml b/src/core/CCArray.ml index 58de07d4..01aaea8e 100644 --- a/src/core/CCArray.ml +++ b/src/core/CCArray.ml @@ -351,12 +351,6 @@ let bsearch ~cmp k a = bsearch ~cmp:CCInt.compare 3 [| |] = `Empty *) -let (>>=) a f = flat_map f a - -let (>>|) a f = map f a - -let (>|=) a f = map f a - let for_all p a = let rec aux i = i = Array.length a || (p a.(i) && aux (i+1)) @@ -716,13 +710,22 @@ let sort_generic (type arr)(type elt) module Infix = struct - let (>>=) = (>>=) - let (>>|) = (>>|) - let (>|=) = (>|=) + let (>>=) a f = flat_map f a + let (>>|) a f = map f a + let (>|=) a f = map f a let (--) = (--) let (--^) = (--^) + + include CCShimsMkLet_.Make(struct + type 'a t = 'a array + let (>>=) = (>>=) + let (>|=) = (>|=) + let monoid_product a1 a2 = monoid_product (fun x y->x,y) a1 a2 + end) end +include Infix + (* test consistency of interfaces *) (*$inject @@ -738,9 +741,3 @@ end (*$R ignore (module CCArray : LL) *) - -include CCShimsMkLet_.Make(struct - type 'a t = 'a array - include Infix - let monoid_product a1 a2 = monoid_product (fun x y->x,y) a1 a2 - end) diff --git a/src/core/CCArray.mli b/src/core/CCArray.mli index b2a07cb0..d6aeac97 100644 --- a/src/core/CCArray.mli +++ b/src/core/CCArray.mli @@ -376,6 +376,10 @@ module Infix : sig val (--^) : int -> int -> int t (** [x --^ y] creates an array containing integers in the range [x .. y]. Right bound excluded. @since 0.17 *) + + (** Let operators on OCaml >= 4.08.0, nothing otherwise + @since NEXT_RELEASE *) + include CCShimsMkLet_.S with type 'a t_let := 'a array end (** Let operators on OCaml >= 4.08.0, nothing otherwise diff --git a/src/core/CCArrayLabels.mli b/src/core/CCArrayLabels.mli index d91fbe1f..6e32b32e 100644 --- a/src/core/CCArrayLabels.mli +++ b/src/core/CCArrayLabels.mli @@ -377,6 +377,10 @@ module Infix : sig val (--^) : int -> int -> int t (** [x --^ y] creates an array containing integers in the range [x .. y]. Right bound excluded. @since 0.17 *) + + (** Let operators on OCaml >= 4.08.0, nothing otherwise + @since NEXT_RELEASE *) + include CCShimsMkLet_.S with type 'a t_let := 'a array end (** Let operators on OCaml >= 4.08.0, nothing otherwise diff --git a/src/core/CCList.ml b/src/core/CCList.ml index 7f7a1200..ea09ddf3 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -123,8 +123,6 @@ let map f l = List.rev (List.rev_map f l) = map f l) *) -let (>|=) l f = map f l - let direct_depth_append_ = 10_000 let cons x l = x::l @@ -548,10 +546,6 @@ let split l = let return x = [x] -let (>>=) l f = flat_map f l - -let (<$>) = map - let pure = return let (<*>) funs l = product (fun f x -> f x) funs l @@ -1710,20 +1704,23 @@ let of_klist l = direct direct_depth_default_ l module Infix = struct - let (>|=) = (>|=) + let (>|=) l f = map f l + let (>>=) l f = flat_map f l let (@) = (@) let (<*>) = (<*>) - let (<$>) = (<$>) - let (>>=) = (>>=) + let (<$>) = map let (--) = (--) let (--^) = (--^) + + include CCShimsMkLet_.Make(struct + type 'a t = 'a list + let (>|=) = (>|=) + let (>>=) = (>>=) + let monoid_product l1 l2 = product (fun x y -> x,y) l1 l2 + end) end -include CCShimsMkLet_.Make(struct - type 'a t = 'a list - include Infix - let monoid_product l1 l2 = product (fun x y -> x,y) l1 l2 - end) +include Infix (** {2 IO} *) diff --git a/src/core/CCList.mli b/src/core/CCList.mli index 08b48c1d..620ca315 100644 --- a/src/core/CCList.mli +++ b/src/core/CCList.mli @@ -738,9 +738,12 @@ module Infix : sig (** Infix alias for [range]. Bounds included. *) val (--^) : int -> int -> int t - (** Infix alias for [range']. Second bound excluded. *) + (** Infix alias for [range']. Second bound excluded. + @since 0.17 *) - (** @since 0.17 *) + (** Let operators on OCaml >= 4.08.0, nothing otherwise + @since NEXT_RELEASE *) + include CCShimsMkLet_.S with type 'a t_let := 'a list end (** Let operators on OCaml >= 4.08.0, nothing otherwise diff --git a/src/core/CCListLabels.mli b/src/core/CCListLabels.mli index 23c538a7..2b8df038 100644 --- a/src/core/CCListLabels.mli +++ b/src/core/CCListLabels.mli @@ -736,9 +736,12 @@ module Infix : sig (** Infix alias for [range]. Bounds included. *) val (--^) : int -> int -> int t - (** Infix alias for [range']. Second bound excluded. *) + (** Infix alias for [range']. Second bound excluded. + @since 0.17 *) - (** @since 0.17 *) + (** Let operators on OCaml >= 4.08.0, nothing otherwise + @since NEXT_RELEASE *) + include CCShimsMkLet_.S with type 'a t_let := 'a list end (** Let operators on OCaml >= 4.08.0, nothing otherwise diff --git a/src/core/CCOpt.ml b/src/core/CCOpt.ml index d307b6d9..dbd8e70c 100644 --- a/src/core/CCOpt.ml +++ b/src/core/CCOpt.ml @@ -38,8 +38,6 @@ let equal f o1 o2 = match o1, o2 with let return x = Some x -let (>|=) x f = map f x - let (>>=) o f = match o with | None -> None | Some x -> f x @@ -55,8 +53,6 @@ let (<*>) f x = match f, x with | _, None -> None | Some f, Some x -> Some (f x) -let (<$>) = map - let or_ ~else_ a = match a with | None -> else_ | Some _ -> a @@ -163,13 +159,24 @@ let of_result = function | Ok x -> Some x module Infix = struct - let (>|=) = (>|=) + let (>|=) x f = map f x let (>>=) = (>>=) let (<*>) = (<*>) - let (<$>) = (<$>) + let (<$>) = map let (<+>) = (<+>) + + include CCShimsMkLet_.Make(struct + type 'a t = 'a option + let (>|=) = (>|=) + let (>>=) = (>>=) + let monoid_product o1 o2 = match o1, o2 with + | Some x, Some y -> Some (x,y) + | _ -> None + end) end +include Infix + type 'a sequence = ('a -> unit) -> unit type 'a gen = unit -> 'a option type 'a printer = Format.formatter -> 'a -> unit @@ -238,11 +245,3 @@ let return_if b x = return_if false 1 = None return_if true 1 = Some 1 *) - -include CCShimsMkLet_.Make(struct - type 'a t = 'a option - include Infix - let monoid_product o1 o2 = match o1, o2 with - | Some x, Some y -> Some (x,y) - | _ -> None - end) diff --git a/src/core/CCOpt.mli b/src/core/CCOpt.mli index c7fbd0b7..b3e3467d 100644 --- a/src/core/CCOpt.mli +++ b/src/core/CCOpt.mli @@ -154,8 +154,17 @@ module Infix : sig val (<+>) : 'a t -> 'a t -> 'a t (** [a <+> b] is [a] if [a] is [Some _], [b] otherwise. *) + (** Let operators on OCaml >= 4.08.0, nothing otherwise + @since NEXT_RELEASE *) + include CCShimsMkLet_.S with type 'a t_let := 'a option + end + +(** Let operators on OCaml >= 4.08.0, nothing otherwise + @since NEXT_RELEASE *) +include CCShimsMkLet_.S with type 'a t_let := 'a option + (** {2 Conversion and IO} *) val to_list : 'a t -> 'a list @@ -199,7 +208,3 @@ val to_seq : 'a t -> 'a sequence @deprecated use {!to_iter} instead *) val pp : 'a printer -> 'a t printer - -(** Let operators on OCaml >= 4.08.0, nothing otherwise - @since NEXT_RELEASE *) -include CCShimsMkLet_.S with type 'a t_let := 'a option diff --git a/src/core/CCResult.ml b/src/core/CCResult.ml index 45f9fd9d..99b2f07e 100644 --- a/src/core/CCResult.ml +++ b/src/core/CCResult.ml @@ -124,10 +124,6 @@ let flat_map f e = match e with | Ok x -> f x | Error s -> Error s -let (>|=) e f = map f e - -let (>>=) e f = flat_map f e - let equal ~err eq a b = match a, b with | Ok x, Ok y -> eq x y | Error s, Error s' -> err s s' @@ -269,11 +265,23 @@ let retry n f = (** {2 Infix} *) module Infix = struct - let (>>=) = (>>=) - let (>|=) = (>|=) + let (>|=) e f = map f e + let (>>=) e f = flat_map f e let (<*>) = (<*>) + + include CCShimsMkLet_.Make2(struct + type ('a,'e) t = ('a,'e) result + let (>>=) = (>>=) + let (>|=) = (>|=) + let monoid_product x1 x2 = match x1, x2 with + | Ok x, Ok y -> Ok (x,y) + | Error e, _ -> Error e + | _, Error e -> Error e + end) end +include Infix + (** {2 Monadic Operations} *) module type MONAD = sig @@ -338,12 +346,3 @@ let pp pp_x fmt e = match e with let pp' pp_x pp_e fmt e = match e with | Ok x -> Format.fprintf fmt "@[ok(@,%a)@]" pp_x x | Error s -> Format.fprintf fmt "@[error(@,%a)@]" pp_e s - -include CCShimsMkLet_.Make2(struct - type ('a,'e) t = ('a,'e) result - include Infix - let monoid_product x1 x2 = match x1, x2 with - | Ok x, Ok y -> Ok (x,y) - | Error e, _ -> Error e - | _, Error e -> Error e - end) diff --git a/src/core/CCResult.mli b/src/core/CCResult.mli index 28e76b7b..320cbec9 100644 --- a/src/core/CCResult.mli +++ b/src/core/CCResult.mli @@ -179,8 +179,17 @@ module Infix : sig (** [a <*> b] evaluates [a] and [b], and, in case of success, returns [Ok (a b)]. Otherwise, it fails, and the error of [a] is chosen over the error of [b] if both fail. *) + + (** Let operators on OCaml >= 4.08.0, nothing otherwise + @since NEXT_RELEASE *) + include CCShimsMkLet_.S2 with type ('a,'e) t_let2 := ('a,'e) result end +(** Let operators on OCaml >= 4.08.0, nothing otherwise + @since NEXT_RELEASE *) +include CCShimsMkLet_.S2 with type ('a,'e) t_let2 := ('a,'e) result + + (** {2 Collections} *) val flatten_l : ('a, 'err) t list -> ('a list, 'err) t @@ -253,7 +262,3 @@ val pp : 'a printer -> ('a, string) t printer val pp': 'a printer -> 'e printer -> ('a, 'e) t printer (** Printer that is generic on the error type. *) - -(** Let operators on OCaml >= 4.08.0, nothing otherwise - @since NEXT_RELEASE *) -include CCShimsMkLet_.S2 with type ('a,'e) t_let2 := ('a,'e) result