Array vs List in Elm

后端 未结 3 1720
感情败类
感情败类 2021-02-12 12:32

I was suprised to learn that Array and List were two different types in Elm:

  • Array

  • List

In my case,

3条回答
  •  死守一世寂寞
    2021-02-12 12:49

    List is a linked list which provides O(n) lookup time based on index. Getting an element by index requires traversing the list over n nodes. An index lookup function for List isn't available in the core library but you can use the elm-community/list-extra package which provides two functions for lookup (varying by parameter order): !! and getAt.

    Array allows for O(log n) index lookup. Index lookups on Array can be done using Array.get. Arrays are represented as Relaxed Radix Balanced Trees.

    Both are immutable (all values in Elm are immutable), so you have trade-offs depending on your situation. List is great when you make a lot of changes because you are merely updating linked list pointers, whereas Array is great for speedy lookup but has somewhat poorer performance for modifications, which you'll want to consider if you're making a lot of changes.

提交回复
热议问题