From 735729c3293da5bde8f237dbb09ef90a87cd9a92 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Tue, 7 Feb 2023 12:07:57 -0500 Subject: [PATCH] add `CCFun.(let@)` (if OCaml >= 4.08) --- src/core/CCFun.ml | 19 +++++++++++++------ src/core/CCFun.mli | 11 +++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/core/CCFun.ml b/src/core/CCFun.ml index 04459c2d..faa408ce 100644 --- a/src/core/CCFun.ml +++ b/src/core/CCFun.ml @@ -85,10 +85,16 @@ let rec iterate n f x = module Infix = struct (* default implem for some operators *) - let ( |> ) x f = f x - let ( @@ ) f x = f x + let ( |> ) = CCShims_.Stdlib.( |> ) + let ( @@ ) = CCShims_.Stdlib.( @@ ) let ( %> ) = compose - let ( % ) f g x = f (g x) + let[@inline] ( % ) f g x = f (g x) + + [@@@ifge 4.8] + + let ( let@ ) = ( @@ ) + + [@@@endif] end include Infix @@ -99,7 +105,8 @@ end) = struct type 'a t = X.t -> 'a - let return x _ = x - let ( >|= ) f g x = g (f x) - let ( >>= ) f g x = g (f x) x + let[@inline] return x _ = x + let[@inline] ( >|= ) f g x = g (f x) + let[@inline] ( >>= ) f g x = g (f x) x end +[@@inline] diff --git a/src/core/CCFun.mli b/src/core/CCFun.mli index 0308aa5e..560171cd 100644 --- a/src/core/CCFun.mli +++ b/src/core/CCFun.mli @@ -9,6 +9,8 @@ include module type of Fun [@@@else_] +(* port from stdlib *) + external id : 'a -> 'a = "%identity" (** This is an API imitating the new standard Fun module *) @@ -99,6 +101,15 @@ module Infix : sig val ( % ) : ('b -> 'c) -> ('a -> 'b) -> 'a -> 'c (** [(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 include module type of Infix