Why no immutable double linked list in Scala colle

2020-06-01 10:48发布

Looking at this question, where the questioner is interested in the first and last instances of some element in a List, it seems a more efficient solution would be to use a DoubleLinkedList that could search backwards from the end of the list. However there is only one implementation in the collections API and it's mutable.

Why is there no immutable version?

5条回答
看我几分像从前
2楼-- · 2020-06-01 10:50

Because you would have to copy the whole list each time you want to make a change. With a normal linked list, you can at least prepend to the list without having to copy everything. And if you do want to copy everything on every change, you don't need a linked list for that. You can just use an immutable array.

查看更多
对你真心纯属浪费
3楼-- · 2020-06-01 10:51

As others have noted, there is no persistent implementation of a double-linked list. You will need some kind of tree to get close to the characteristics you want.

In particular, you may want to look at finger trees, which provide O(1) access to the front and back, amortized O(1) insertion to the front and back, and O(log n) insertion elsewhere. (That's in contrast to most other commonly-used trees which have O(log n) access and insertion everywhere.)

See also:

查看更多
家丑人穷心不美
4楼-- · 2020-06-01 10:59

Because it is logically impossible to create a mutually (circular) referential data-structure with strict immutability.

You cannot create two nodes that point to each other due to simple existential ordering priority, in that at least one of the nodes will not exist when the other is created.

It is possible to get this circularity with tricks involving laziness (which is implemented with mutation), but the real question then becomes why you would want this thing in the first place?

查看更多
beautiful°
5楼-- · 2020-06-01 11:04

As a supplemental to the answer of @KimStebel I like to add:
If you are searching for a data structure suitable for the question that motivated you to ask this question, then you might have a look at Extreme Cleverness: Functional Data Structures in Scala by @DanielSpiewak.

查看更多
看我几分像从前
6楼-- · 2020-06-01 11:15

There are many impediments to such a structure, but one is very pressing: a doubly linked list cannot be persistent.

The logic behind this is pretty simple: from any node on the list, you can reach any other node. So, if I added an element X to this list DL, and tried to use a part of DL, I'd face this contradiction: from the node pointing to X one can reach every element in part(DL), but, by the properties of the doubly linked list, that means from any element of part(DL) I can reach the node pointing to X. Since part(DL) is supposed to be immutable and part of DL, and since DL did not include the node pointing to X, that just cannot be.

Non-persistent immutable data structures might have some uses, but they are generally bad for most operations, since they need to be recreated whenever a derivative is produced.

Now, there's the minor matter of creating mutually referencing strict objects, but this is surmountable. One can use by-name parameters and lazy vals, or one can do like Scala's List: actually create a mutable collection, and then "freeze" it in immutable state (see ListBuffer and it's toList method).

查看更多
登录 后发表回答