2
votes

Clojure is using persistent data structures , is there a way to access older versions of vector or maps since it is keeping it internally ?

Lets say for a Vector ,what i meant is since clojure is not copying full structure and keeping it in a tree internally (see https://hypirion.com/musings/understanding-persistent-vector-pt-1) and keeps older structure values too, is there a way to use this to do some senarios like undo/redo or replay, It is using the same principle for Datomic to retrieve older version for data, so im asking if it is possible to get this in clojure.

3
What do you mean by "older versions of vector"? Objects that you don't hold a reference to?Carcigenicate
You could store a vector in an atom and use add-watch to subscribe to changes, in the subscriber conj each new state on to a list or vector stored in another atom...Kris

3 Answers

0
votes

Any version of a persistent data structure survives so long as there is a live reference to it. Thereafter, it is subject to garbage collection.

The Clojure persistent vectors and maps are like copy-on-write file systems such as Btrfs, both in concept and in the sort of internal data structures they employ to create the illusion that each version of an entity is quite distinct.

0
votes

I am not sure that I understood the question, but just keep a reference on the old structure.

(def myOldMap {a 1 b 2 c3})
(def myNewMap (assoc myOldMap b 7))
0
votes

As per Kris's comment, using add-watch is the right answer it seems , David Nolen has described this approach here https://swannodette.github.io/2013/12/31/time-travel