Persistent Data Structures for Objective-C
github.com
github.com
One thing to remember is that getting a new version of the data structure does not change old ones at all. In order to change state, in Clojure for example, you use constructs such as atoms to "advance" a persistent data structure forward. Think about it like advancing through your git commit history - efficiently and by only applying the delta for the next "change".
Your classes are 10-20x slower in every other way, which definitely would offset any imaginary expense you think calling `mutableCopy` costs.
I have to tell you in 10+ years of writing Objective-C where I was like [myArray mutableCopy] is super slow, I need a replacement. I've wished for an NSOrderedDictionary though.
Then,
// yourDictionary is NSMutableDictionary, [yourDictionary count] > 20
NSMutableDictionary *mutable = [yourDictionary mutableCopy];
mutable[@"old_key"] = @"new_value";
NSDictionary *changedDict = [NSDictionary dictionaryWithDictionary:mutable];
will be slower than // yourDictionary is AAPersistentHashMap, [yourDictionary count] > 20
AAPersistentHashMap *changedDict = [yourDictionary setObject:@"new_value" forKey:@"old_key"];
Remember, you need to get another immutable map, which is a modified version of the original, preferably memory-efficient, which means we want to reuse the unchanged keys/values of the original map.