2 ms·
Can someone give me a good example of where immutable data structures are better than mutable ones. I know that you are more likely to mess up and have side eff
by collyw 12y ago
Can someone give me a good example of where immutable data structures are better than mutable ones. I know that you are more likely to mess up and have side effects with mutable data structures, but so far all I have heard is theory.
- TheLoneWolfling 12y agoWell, for one thing you don't need to make defensive copies all over the place. Look at OBJ05-J and OBJ06-J, for example. Some of these copies can be removed via optimization, but not always.
- couchand 12y agoThe big win is they enable lazy evaluation: you can defer the evaluation of an expression until it's needed, or discard the result and compute it again later, or cache the result of an expression and not need to re-evaluate, a flexibility which can be useful in many situations. You also get undo/redo stacks basically for free, since you simply maintain references to the previous n immutable states. As Om demonstrates, they also make possible significant optimizations of code that takes various view of the same underlying data, like rendering routines.