列表与元组-使用什么时候? [英] Lists vs. Tuples - What to use and when?

查看:112
本文介绍了列表与元组-使用什么时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试掌握 Elixir 列表组合的区别。从 基本类型 Elixir指南的a>部分:

I am trying to grasp the difference between Lists and Tuples in Elixir. From the Basic Types section of Elixir Guides, I understand that:


  • 列表存储为链接项

  • 列表的更新速度很快(仅在添加时)

  • 获取列表项很慢

  • 获取列表信息(大小/长度)很慢

  • 元组元素存储在一起

  • 快速获取元组信息

  • 快速获取元组元素

  • 修改元组很昂贵

  • Lists are stored as Linked Items
  • Updating a List is fast (only when prepending)
  • Fetching List items is slow
  • Fetching List information (size/length) is slow
  • Tuple Elements are stored together
  • Getting Tuple information is fast
  • Fetching Tuple elements is fast
  • Modifying Tuples is expensive

好的,但是我仍然不确定何时使用。我看到大多数方法都返回一个元组,但在其他所有地方都使用列表,并且许多方法都将列表而不是元组作为输入。根据上述要点,元组不应该用于传递数据,因为从元组中读取用户提供的值会很快吗?

Okay, that's all fine but I'm still not sure what to use when. I see that most of the methods return a tuple but everywhere else Lists are used, and many methods accept Lists as input, not tuples. By the stated points above, shouldn't Tuples be used for passing data around, since reading from a tuple of user-given values would be fast?

我还注意到元组不能枚举,这是怎么回事?在他们上使用 Enum 是否比在列表上使用它更快?

I also noticed Tuples aren't enumerable, what's up with that? Wouldn't using Enum over them be faster than using it on Lists?

如果有人可以帮助我理解它们更好,可能会提供一些示例,说明使用时会很棒。

If someone could help me understand them better, possibly by giving a few examples of what to use when, that'd be awesome.

推荐答案

您已经给出了很好地总结了这些差异,因此,在其中任何一件重要的条件下,它都应该帮助您决定使用哪一种。

You've already given a pretty good summary of the differences, so in any conditions where one of those things is important it should help you decide on which to use.

思考方式是列表是开放式数据结构,并且它们的大小可以在运行时变化,而元组在编译时设置的大小是恒定的。

The way to think about it is that Lists are open-ended data structures and their size can vary at runtime while Tuples have a constant size set at compile time.

例如,如果要存储所有用户想要在 iex 会话中给出的命令,该列表的长度取决于该会话中给出的命令数。将其与元组的典型用例进行对比-返回 {:ok,result} {:error,reason} 从方法上讲-这里的元素数量是预先知道的,因此您不必为提高Tuples的性能付出不可接受的代价。

For example if you wanted to store all the commands the user has given during an iex session you would want a list - the length of that list will depend on the number of commands given in that session. Contrast this with a typical use-case for tuples - returning {:ok, result} or {:error, reason} from a method - here the number of elements is known up-front and so you don't pay an unacceptable price for the performance improvements of Tuples.

至于枚举-从概念上讲,元组不是集合,并且每个元素的位置都应该表示其作用。考虑一个 {:ok,#PID< 0.336.0>}} 元组-对其进行迭代将首先给您一个:ok 然后是#PID< 0.336.0> ,编写一个以统一方式作用于这些东西的函数将非常奇怪。

As for enumeration - Tuples conceptually aren't collections and every element's position is supposed to also denote its role. Consider an {:ok, #PID<0.336.0>} Tuple - iterating over it would first give you an :ok and then a #PID<0.336.0>, it would be very strange to write a function acting in a uniform way on those things.

这篇关于列表与元组-使用什么时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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