How is memory-efficient non-destructive manipulation of collections achieved in functional programming?

前端 未结 5 738
孤城傲影
孤城傲影 2020-12-23 22:22

I\'m trying to figure out how non-destructive manipulation of large collections is implemented in functional programming, ie. how it is possible to alter or remove single el

5条回答
  •  旧巷少年郎
    2020-12-23 22:42

    While the referenced objects are the same in your code, I beleive the storage space for the references themselves and the structure of the list is duplicated by take. As a result, while the referenced objects are the same, and the tails are shared between the two lists, the "cells" for the initial portions are duplicated.

    I'm not an expert in functional programming, but maybe with some kind of tree you could achieve duplication of only log(n) elements, as you would have to recreate only the path from the root to the inserted element.

提交回复
热议问题