榆木中的阵列与列表 [英] Array vs List in Elm

查看:75
本文介绍了榆木中的阵列与列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很惊讶地发现Elm中ArrayList是两种不同的类型:

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

List

就我而言,我的List Int长度为2,000,000,我需要大约10,000,但是我事先不知道哪一万.那将由另一个列表提供.用伪代码:

In my case, I have a List Int of length 2,000,000 and I need about 10,000 of them but I don't know in advance which ten thousand. That will be provided by another list. In pseudo-code:

x = [ 1,1,0,30,...,255,0,1 ]
y = [ 1,4,7,18,36,..., 1334823 , ... 1899876 ]
z = [ y[x[0]], y[x[1]], ... ]

我正在使用伪代码,因为显然这不是Elm语法(它可能是合法的JavaScript).

I am using pseudocode because clearly this isn't Elm syntax (it might be legal JavaScript).

这些数组选择可以在ListArray或两者中进行吗?

Can these array selections be done in List or Array or both?

推荐答案

List是一个链表,它基于索引提供O(n)查找时间.通过索引获取元素需要遍历n节点上的列表. List的索引查找功能在核心库中不可用,但是您可以使用 !!

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允许 O(log n)索引查找.可以使用 Array.get Array上的索引查找>.数组表示为轻松的基数平衡树.

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.

两者都是不可变的(榆树中的所有值都是不可变的),因此您需要根据情况进行权衡. List在进行大量更改时非常有用,因为您只是在更新链表指针,而Array对于快速查找非常有用,但对修改的性能却较差,如果要进行修改,您需要考虑一下很多变化.

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.

这篇关于榆木中的阵列与列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆