System`Utilities`HashTable
is conceptually a lot like an Association
but it has the pretty phenomenal (for Mathematica) property that it's properly mutable. What I mean is that if we have a hash-table and bind it to a variable, assign it to a new variable, and mutate it there, we get the changes showing up on our original hash-table:
ht = System`Utilities`HashTable[]
System`Utilities`HashTable["<" 0 ">"]
b = ht;
System`Utilities`HashTableAdd[ht, "key", val];
ht
System`Utilities`HashTable["<" 1 ">"]
By contrast Association
copies itself on assignment so this doesn't work:
ht = <||>;
b = ht;
b["key"] = val;
ht
<||>
For this an other efficiency-related reasons I'd like to use HashTable
a lot more than I currently do.
One stumbling block is that the interface is annoying to use, to say the least.
Can we make this work a lot more like Association
?
Association
is rather large, because it is integrated well with the language (so supports things likeMap
,Join
, etc.). All that is only possible because it is immutable. For hash table being mutable, the only option you have is to may be add some interface to standard create, put, get, contains operations. Anything else, mixing it with immutable expressions, will probably just be a lot of pain at the end. $\endgroup$HashTable
with reasonable integration into the language. It doesn't have to beAssociation
quality, but really just decent is good enough. Mutability is a useful property if you know what you're doing and so good enough is still very useful. $\endgroup$Map
should not have side effects. $\endgroup$Map
the function shouldn't mutate a structure, but"Map"
the method can. $\endgroup$