From 669ac752c869ea1cd1ac1f1933bbbc0794d51297 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Tue, 19 Dec 2023 23:26:25 -0700 Subject: [PATCH] refactor: add [{Fiber,Memo}.of_thunk_apply] Signed-off-by: Rudi Grinberg --- src/memo/memo.mli | 7 +++++++ vendor/fiber/src/core.ml | 1 + vendor/fiber/src/fiber.mli | 9 ++++++--- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/memo/memo.mli b/src/memo/memo.mli index 1bbd7d35986..3f23d2a8746 100644 --- a/src/memo/memo.mli +++ b/src/memo/memo.mli @@ -57,8 +57,15 @@ val of_reproducible_fiber : 'a Fiber.t -> 'a t therefore be re-executed on every build run. *) val of_non_reproducible_fiber : 'a Fiber.t -> 'a t +(** Convert a thunk to a Memo computation, making sure the thunk runs in the context of + the Memo computation rather than in the current context. + + [of_thunk f] is equivalent to [return () >> f] but is more explicit. *) val of_thunk : (unit -> 'a t) -> 'a t +(** Like [of_thunk] but accepts functions of any argument. *) +val of_thunk_apply : ('a -> 'b t) -> 'a -> 'b t + (** Combine results of two computations executed in sequence. *) val both : 'a t -> 'b t -> ('a * 'b) t diff --git a/vendor/fiber/src/core.ml b/vendor/fiber/src/core.ml index 7cde5272ef6..80152c6c496 100644 --- a/vendor/fiber/src/core.ml +++ b/vendor/fiber/src/core.ml @@ -245,6 +245,7 @@ module Var = struct end let of_thunk f k = f () k +let of_thunk_apply f x k = f x k module O = struct let ( >>> ) a b k = a (fun () -> b k) diff --git a/vendor/fiber/src/fiber.mli b/vendor/fiber/src/fiber.mli index efdfe8a5c92..8fbac56fec5 100644 --- a/vendor/fiber/src/fiber.mli +++ b/vendor/fiber/src/fiber.mli @@ -18,12 +18,15 @@ type 'a fiber := 'a t (** Create a fiber that has already terminated. *) val return : 'a -> 'a t -(** Converts a thunk to a fiber, making sure the thunk runs in the context of - the fiber (rather than applied in the current context). +(** Convert a thunk to a fiber, making sure the thunk runs in the context of the fiber + rather than in the current context. - Equivalent to [(>>=) (return ())], but more explicit. *) + [of_thunk f] is equivalent to [return () >>= f] but is more explicit. *) val of_thunk : (unit -> 'a t) -> 'a t +(** Like [of_thunk] but accepts functions of any argument. *) +val of_thunk_apply : ('a -> 'b t) -> 'a -> 'b t + (** Fiber that never completes. *) val never : 'a t