Re:Rich Hickey and Brian Beckman – Inside Clojure (2009) [vi...
by slifin on Friday March 26, 2021 @05:44AM
(#26590278)
Internally those are "persistent data structures" in Clojure which don't duplicate the data but instead mutate a underlying tree that links both the old state and the new state
Then as a developer you are looking into a certain piece of that tree depending on what bit of the "mutated value" you are viewing
I assume this is to help ensure memory usage doesn't balloon but if you need to do mutation there are non-default features for doing that: https://clojure.org/reference/transients with all the pros and cons of mutation
http://www.kylheku.com/cgit/txr/tree/share/txr/stdlib/compil...
Notice how there is a method to take a snapshot of the compiler state, which includes making copies of several hash tables, and a corresponding restore method to put it back. Hey, isn't there some GoF Design Pattern for this? Memento or something.
The thing is, if hash tables and objects were immutable, the whole code would be a shitshow, and perform badly too.
It would just not have that one problem: it would never have to copy a hash table in order to have a copy of the old one.
Immutable HAMT hash tables that I could conj to and then assign back into the original location would be a reasonable middle ground. Then I would still need he snapshot method; but it wouldn't have to do anything beyond returning (copy me).
It occurs to me I could implement a shallow hash table copy function, such that the original and new table share chains. Mutating either table (overwriting an existing key/value pair with a new value) would show up in the other, but deletions and insertions would not.