3 ms·
Why do you think that functional programming results in data duplication? I would think it's rather the opposite. I suspect it has something to do with the per
by bmj 4y ago
Why do you think that functional programming results in data duplication? I would think it's rather the opposite.
I suspect it has something to do with the perceptions around always returning a new thing from a function rather than the mutated input to the function. For example, if you need to mutate the property of an object based on other inputs, the perception is you would clone that object, modify the property appropriately, and then return the cloned object.
[Edit: formatting]
- AnimalMuppet 4y agoSo, ELI5: What do you do instead, where you return a modified object, but still have immutability? Or do you avoid the problem by not trying to do that?
- hither_shores 4y agoIn most situations, you use persistent data structures where you only have to copy the modified leaves. If you really need your type to be backed by a contiguous block of memory, you batch updates (stencils, SIMD, etc.)
- AnimalMuppet 4y agoBut if you modify the leaf, don't you also have to modify the branch node that points to the leaf, so that it points to the new one? And every node from there to the root?
- hither_shores 4y agoYes, you (or rather, core library developers) need to pick a data structure appropriate to your access patterns: linked lists for iteration, search trees for concatenation, finger trees for random access, and so on. But you should be doing this anyway for clarity, even if you face no performance constraints.