列表、元組、關(guān)鍵字列表(keywords)、圖(maps)、字典和函數(shù)組合子(combinators)
目錄
- 列表
- 列表拼接
- 列表減法
- 頭/尾
- 元組
- 關(guān)鍵字列表
- 圖
列表
列表是值的簡(jiǎn)單集合,可以包括不同的數(shù)據(jù)類型,而且可能包含相同的值。
iex> [3.14, :pie, "Apple"]
[3.14, :pie, "Apple"]
Elixir 內(nèi)部用鏈表實(shí)現(xiàn)列表,這表明獲取列表長(zhǎng)度是O(n)的操作。同樣的原因,在頭部插入比在尾部插入要快。
iex> list = [3.14, :pie, "Apple"]
[3.14, :pie, "Apple"]
iex> ["π"] ++ list
["π", 3.14, :pie, "Apple"]
iex> list ++ ["Cherry"]
[3.14, :pie, "Apple", "Cherry"]
列表的拼接
列表拼接使用 ++/2 操作符:
iex> [1, 2] ++ [3, 4, 1]
[1, 2, 3, 4, 1]
++/2的格式說(shuō)明:在 Elixir 中(以及 Elixir 的基礎(chǔ)語(yǔ)言 Erlang),函數(shù)和操作符的名字由兩部分組成:名字(比如這里的
++)和元數(shù)。元數(shù)是 Elixir 和 Erlang 代碼非常核心的部分,它代表了給定行數(shù)接受的參數(shù)個(gè)數(shù)(比如這里的 2),元數(shù)和名字之間通過(guò)斜線分割。
列表減法
--/2 操作符支持列表的減法,而且減去不存在的值也是安全的。
iex> ["foo", :bar, 42] -- [42, "bar"]
["foo", :bar]
要注意重復(fù)值的處理:對(duì)于左邊列表中 的每個(gè)值,右邊只有首次出現(xiàn)的這個(gè)值會(huì)被刪除:
iex> [1,2,2,3,2,3] -- [1,2,3,2]
[2, 3]
頭/尾
使用列表的時(shí)候,經(jīng)常要和頭部和尾部打交道:列表的頭部是列表的第一個(gè)元素;尾部是除去第一個(gè)元素剩下的列表。 Elixir 提供了兩個(gè)函數(shù) hd 和 tl 來(lái)獲取這兩個(gè)部分。
iex> hd [3.14, :pie, "Apple"]
3.14
iex> tl [3.14, :pie, "Apple"]
[:pie, "Apple"]
除了上面的提到的函數(shù),你還可以使用模式匹配(pattern matching) 和 | 操作符來(lái)把一個(gè)列表分成頭尾兩部分;
iex> [head | tail] = [3.14, :pie, "Apple"]
[3.14, :pie, "Apple"]
iex> head
3.14
iex> tail
[:pie, "Apple"]
元組
元組和列表很像,但元組在內(nèi)存是連續(xù)存放的。這樣,獲取元組的長(zhǎng)度很快,但修改元組的操作很昂貴,新的元組必須重新在內(nèi)存中拷貝一份。定義元組要用花括號(hào):
iex> {3.14, :pie, "Apple"}
{3.14, :pie, "Apple"}
元組一個(gè)很常見的用法是作為函數(shù)的返回值,來(lái)返回額外的信息,當(dāng)介紹到模式匹配的時(shí)候,這種用法的好處就顯而易見了。
iex> File.read("path/to/existing/file")
{:ok, "... contents ..."}
iex> File.read("path/to/unknown/file")
{:error, :enoent}
關(guān)鍵字列表
關(guān)鍵字列表(keywords)和圖(maps)是Elixir 中兩個(gè)相關(guān)的集合,Elixir 的關(guān)鍵字列表是一種獨(dú)特的列表:列表里的內(nèi)容是二元元組,并且二元組的第一個(gè)元素必須是原子。它和列表的行為完全一致。
iex> [foo: "bar", hello: "world"]
[foo: "bar", hello: "world"]
iex> [{:foo, "bar"}, {:hello, "world"}]
[foo: "bar", hello: "world"]
關(guān)鍵字列表非常重要,它有以下特性:
- 鍵都是原子
- 鍵是有序的(定義后,順序不會(huì)改變)
- 鍵不是唯一的
因?yàn)檫@些特性,關(guān)鍵字列表最常見的用法是作為參數(shù)傳遞給函數(shù)。
圖
Elixir 的圖示鍵值對(duì)結(jié)構(gòu)的第一選擇,和關(guān)鍵字列表不通,圖允許任意類型的數(shù)據(jù)作為鍵,而且數(shù)據(jù)并不嚴(yán)格排序。你可以使用%{}來(lái)定義圖:
iex> map = %{:foo => "bar", "hello" => :world}
%{:foo => "bar", "hello" => :world}
iex> map[:foo]
"bar"
iex> map["hello"]
:world
Elixir 1.2 版本中,也可以把變量作為圖的鍵:
iex> key = "hello"
"hello"
iex> %{key => "world"}
%{"hello" => "world"}
如果重復(fù)的鍵添加到圖中,后面的值會(huì)覆蓋之前的值:
iex> %{:foo => "bar", :foo => "hello world"}
%{foo: "hello world"}
從上面的運(yùn)行結(jié)果可以看出,存儲(chǔ)鍵只有原子的圖,可以不用 =>,直接使用關(guān)鍵字列表的語(yǔ)法:
iex> %{foo: "bar", hello: "world"}
%{foo: "bar", hello: "world"}
iex> %{foo: "bar", hello: "world"} == %{:foo => "bar", :hello => "world"}
true
圖的另一個(gè)特性是:它們提供令人自己更新和獲取原子鍵的語(yǔ)法:
iex> map = %{foo: "bar", hello: "world"}
%{foo: "bar", hello: "world"}
iex> %{map | foo: "baz"}
%{foo: "baz", hello: "world"}
iex> map.hello
"world"
iex> map[hello:]
"world"