Why no immutable double linked list in Scala collections?

后端 未结 5 638
轻奢々
轻奢々 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: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)

提交回复
热议问题