4 ms·
Problems like this are why I only try for 'outwardly' functional code. Without knowing the full details, I'd speculate that they only care about the image befo
by warrenwilkinson 16y ago
Problems like this are why I only try for 'outwardly' functional code.
Without knowing the full details, I'd speculate that they only care about the image before and after modification. So within the function, you have free reign. For example if you just destructively modify and return a copy of the source, to the outside caller it appears functional. And everything is blessedly simple.
- DannoHung 16y agoThere are some reasons for doing it this way, in particular, such a structure can make undo operations very simple.
- dons 16y ago> if you just destructively modify and return a copy of the source Then the caller can no longer reference the source without observing the fact you've mutated it -- you'd have to guarantee they've dropped all references to the now-mutated buffer. How do you ensure the mutation doesn't leak out like this, breaking all the nice properties?
- ricree 16y agoI think that he means the copy that gets returned is what gets destructively modified. In other words, the overall process winds up being functional and nondestructive, but the intermediate internal steps aren't necessarily so.
- dons 16y agoRight. You better copy the original data structure. That's one good way to guarantee internal mutation (of the copy) isn't going to be visible. Or it gets tricky (e.g. linear types)