From 8b66c0bc3515a3bd1a8488dab23d974a01a080b2 Mon Sep 17 00:00:00 2001 From: Olivier Nicole Date: Mon, 20 Nov 2023 14:14:47 +0100 Subject: [PATCH 1/2] Add Lwd.map_and_set --- lib/lwd/lwd.ml | 2 ++ lib/lwd/lwd.mli | 3 +++ 2 files changed, 5 insertions(+) diff --git a/lib/lwd/lwd.ml b/lib/lwd/lwd.ml index 3c77a27..f57c6de 100644 --- a/lib/lwd/lwd.ml +++ b/lib/lwd/lwd.ml @@ -308,6 +308,8 @@ let peek = function | Operator ({desc = Var v; _}) -> v.binding | _ -> assert false +let map_and_set f v = set v (f (peek v)) + (* Primitives *) type 'a prim = 'a t let prim ~acquire ~release = diff --git a/lib/lwd/lwd.mli b/lib/lwd/lwd.mli index 386dde3..77ff1d3 100644 --- a/lib/lwd/lwd.mli +++ b/lib/lwd/lwd.mli @@ -67,6 +67,9 @@ val peek : 'a var -> 'a (** Observe the current value of the variable, without any dependency tracking. *) +val map_and_set : ('a -> 'a) -> 'a var -> unit +(** Modify a variable based on its currently observed value. *) + type +'a prim (** A primitive document. It can correspond, for example, to a primitive UI element. From 99bb0ba6e596096e9ad206f41fcd6457ea34824a Mon Sep 17 00:00:00 2001 From: Olivier Nicole Date: Mon, 20 Nov 2023 14:57:06 +0100 Subject: [PATCH 2/2] Rename Lwd.map_and_set -> Lwd.update --- lib/lwd/lwd.ml | 2 +- lib/lwd/lwd.mli | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/lwd/lwd.ml b/lib/lwd/lwd.ml index f57c6de..4fb8ad5 100644 --- a/lib/lwd/lwd.ml +++ b/lib/lwd/lwd.ml @@ -308,7 +308,7 @@ let peek = function | Operator ({desc = Var v; _}) -> v.binding | _ -> assert false -let map_and_set f v = set v (f (peek v)) +let update f v = set v (f (peek v)) (* Primitives *) type 'a prim = 'a t diff --git a/lib/lwd/lwd.mli b/lib/lwd/lwd.mli index 77ff1d3..e42389f 100644 --- a/lib/lwd/lwd.mli +++ b/lib/lwd/lwd.mli @@ -67,7 +67,7 @@ val peek : 'a var -> 'a (** Observe the current value of the variable, without any dependency tracking. *) -val map_and_set : ('a -> 'a) -> 'a var -> unit +val update : ('a -> 'a) -> 'a var -> unit (** Modify a variable based on its currently observed value. *) type +'a prim