Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why no immutable double linked list in Scala collections?

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?

like image 315
Luigi Plinge Avatar asked Nov 07 '11 20:11

Luigi Plinge


People also ask

Is Scala list immutable?

Specific to Scala, a list is a collection which contains immutable data, which means that once the list is created, then it can not be altered. In Scala, the list represents a linked list. In a Scala list, each element need not be of the same data type.

How do you make a linked list immutable?

In Java, use of() with Set, Map or List to create an Immutable List.

Why is double linked list better?

If we need better performance while searching and memory is not a limitation in this case doubly linked list is more preferred. As singly linked list store pointer of only one node so consumes lesser memory. On other hand Doubly linked list uses more memory per node(two pointers).

Is double linked list important?

It is used in the navigation systems where front and back navigation is required. It is used by the browser to implement backward and forward navigation of visited web pages that is a back and forward button. It is also used to represent a classic game deck of cards.


2 Answers

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.

like image 191
Kim Stebel Avatar answered Sep 18 '22 12:09

Kim Stebel


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).

like image 21
Daniel C. Sobral Avatar answered Sep 18 '22 12:09

Daniel C. Sobral