How would fold over an incremental type in Incremental

Let’s say I have the following type:

type diff = Foo of int | Bar of string

which represents diff updates of a complex data structure. I have another object, baz : int, that is listening to the diff updates and updates to its values based on the diff.

Specifically, baz updates with the following rule

| Foo x ->  baz := !baz + x
| Bar x ->  baz := !baz + (String.length x)

The update that baz needs to perform is a fold update whenever diff changes. I was looking at the Incremental API for this type fold update. However, there is no clear way to do this without exposing mutation. Is there a way to do this without exposing any mutation?

I’m not sure I fully understand your use case, but the “with_old” function you find here might be helpful:

It lets you write a more optimized incremental function of a changing value by giving you access to previous versions of the computation, so you can potentially diff the inputs and figure out how to efficiently update the output from the result. That amounts to a kind of a simple temporal fold, but I’m not sure it quite achieves what you want.

1 Like

Okay, great. I think this will do the trick. Thanks Yaron.