add CCFun.(let@) (if OCaml >= 4.08)

This commit is contained in:
Simon Cruanes 2023-02-07 12:07:57 -05:00
parent b1c39832aa
commit 735729c329
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 24 additions and 6 deletions

View file

@ -85,10 +85,16 @@ let rec iterate n f x =
module Infix = struct module Infix = struct
(* default implem for some operators *) (* default implem for some operators *)
let ( |> ) x f = f x let ( |> ) = CCShims_.Stdlib.( |> )
let ( @@ ) f x = f x let ( @@ ) = CCShims_.Stdlib.( @@ )
let ( %> ) = compose let ( %> ) = compose
let ( % ) f g x = f (g x) let[@inline] ( % ) f g x = f (g x)
[@@@ifge 4.8]
let ( let@ ) = ( @@ )
[@@@endif]
end end
include Infix include Infix
@ -99,7 +105,8 @@ end) =
struct struct
type 'a t = X.t -> 'a type 'a t = X.t -> 'a
let return x _ = x let[@inline] return x _ = x
let ( >|= ) f g x = g (f x) let[@inline] ( >|= ) f g x = g (f x)
let ( >>= ) f g x = g (f x) x let[@inline] ( >>= ) f g x = g (f x) x
end end
[@@inline]

View file

@ -9,6 +9,8 @@ include module type of Fun
[@@@else_] [@@@else_]
(* port from stdlib *)
external id : 'a -> 'a = "%identity" external id : 'a -> 'a = "%identity"
(** This is an API imitating the new standard Fun module *) (** This is an API imitating the new standard Fun module *)
@ -99,6 +101,15 @@ module Infix : sig
val ( % ) : ('b -> 'c) -> ('a -> 'b) -> 'a -> 'c val ( % ) : ('b -> 'c) -> ('a -> 'b) -> 'a -> 'c
(** [(f % g) x] or [(%) f g x] is [f (g x)]. Mathematical composition. *) (** [(f % g) x] or [(%) f g x] is [f (g x)]. Mathematical composition. *)
[@@@ifge 4.8]
val ( let@ ) : ('a -> 'b) -> 'a -> 'b
(** [let@ x = foo in bar] is the equivalent of [foo @@ fun x -> bar].
It can be very useful for resource management, alongside with {!protect}.
@since NEXT_RELEASE *)
[@@@endif]
end end
include module type of Infix include module type of Infix