Why no immutable double linked list in Scala collections?

后端 未结 5 637
轻奢々
轻奢々 2021-02-05 16:33

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

相关标签:
5条回答
  • 2021-02-05 17:13

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

    0 讨论(0)
  • 2021-02-05 17:15

    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.

    0 讨论(0)
  • 2021-02-05 17:23

    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:

    • video explanation of finger trees (by the implementor of finger trees in clojure.contrib)
    • finger tree implementation in Scala (I haven't used it personally, but it's the top google hit)
    0 讨论(0)
  • 2021-02-05 17:32

    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?

    0 讨论(0)
  • 2021-02-05 17:36

    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.

    0 讨论(0)
提交回复
热议问题