4 ms·
> and you have some function that can re-build the current state from that list... You can be far mor efficient than that. The trick is to use immutable data
by unoti 9y ago
> and you have some function that can re-build the current state from that list...
You can be far mor efficient than that. The trick is to use immutable data structures to represent your state. When something changes, return a new state. Parts of the state that didn’t change are still referring to chunks from the previous state, so it’s actually sharing most of the memory from the previous state.
There are data structures in libraries in different languages that support this idea, for example system.collections.immutable in .NET. Anything you build in Erlang or Elixir will basically automatically do this. (As an aside, Erlang IOlists are a related concept to the piece tables the article talks about.)
Using this approach you don’t actually have to rebuild a previous or redo state, you just keep a list of states and move between them for undo and redo.