官术网_书友最值得收藏!

Tuples

Tuples are used to group a fixed number of elements together. They can hold any value—even other tuples. They are stored contiguously in memory, which provides constant access time to elements inside a tuple. You create a tuple surrounding the elements with curly brackes ({ and }), and separate the elements with commas:

iex> {:ok, 3.14}
{:ok, 3.14}

A common usage of tuples in Elixir is to pattern-match on the result of a function to ensure its success (usually with an :ok atom) or deal with an error. We will be looking to pattern matching and functions later in this chapter.

To access an element inside a tuple, we use the elem function (from the Kernel module), providing the tuple and a zero-based index:

iex> result = {:ok, 3.14}
{:ok, 3.14}
iex> elem(result, 1)
3.14
Functions from the Kernel module are auto-imported. Thus, we don't need to prefix them with the module name.

To change the elements on a tuple, you can use the put_elem function. The arguments are similar to the elem function, but you also provide the new value for that position of the tuple:

iex> put_elem(result, 1, 1.61)
{:ok, 1.61}
iex> result
{:ok, 3.14}

Notice how the result variable hasn't changed. As we discussed in the beginning of this chapter, data in Elixir is immutable. As such, although we've updated the tuple with a new value, the original tuple hasn't changed—Elixir updated the value on a copy of the original tuple. This way our code is side-effect free, and any other function holding a reference to the result variable won't have any surprises.

The general recommendation in Elixir is that tuples should hold up to four elements—anything more than that and you probably should be using another type.

主站蜘蛛池模板: 静宁县| 鹤岗市| 淮北市| 平昌县| 桐城市| 六盘水市| 苍梧县| 乌审旗| 平乐县| 沿河| 汉寿县| 威远县| 财经| 岑溪市| 吉木乃县| 克东县| 浪卡子县| 怀安县| 绥滨县| 襄垣县| 正蓝旗| 西丰县| 潍坊市| 上杭县| 普格县| 峨眉山市| 肇州县| 顺平县| 景洪市| 马关县| 景泰县| 深水埗区| 阿勒泰市| 上林县| 荆州市| 龙岩市| 同仁县| 临安市| 屏东县| 清新县| 卢湾区|