檢視原始碼 清單 (Elixir v1.16.2)
連結清單依選定順序儲存零個、一個或多個元素。
Elixir 中的清單以方括號指定
iex> [1, "two", 3, :four]
[1, "two", 3, :four]
兩個清單可以使用 ++/2
和 --/2
算子進行串接和減法
iex> [1, 2, 3] ++ [4, 5, 6]
[1, 2, 3, 4, 5, 6]
iex> [1, true, 2, false, 3, true] -- [true, false]
[1, 2, 3, true]
可以使用 |
將元素置於清單之前
iex> new = 0
iex> list = [1, 2, 3]
iex> [new | list]
[0, 1, 2, 3]
Elixir 中的清單實際上是連結清單,這表示它們在內部以包含清單的頭部和尾部的成對方式表示
iex> [head | tail] = [1, 2, 3]
iex> head
1
iex> tail
[2, 3]
類似地,我們可以使用此類成對 (稱為 cons 單元) 來撰寫清單 [1, 2, 3]
iex> [1 | [2 | [3 | []]]]
[1, 2, 3]
某些稱為不當清單的清單,其最後 cons 單元的第二個元素並非空清單
iex> [1 | [2 | [3 | 4]]]
[1, 2, 3 | 4]
雖然通常會避免使用不當清單,但它們會在某些特殊情況下使用,例如 iodata 和 chardata 實體 (請參閱 IO
模組)。
由於它們基於 cons 單元的表示,將元素置於清單之前總是很快 (恆定時間),而隨著清單大小的增加,附加會變得更慢 (線性時間)
iex> list = [1, 2, 3]
iex> [0 | list] # fast
[0, 1, 2, 3]
iex> list ++ [4] # slow
[1, 2, 3, 4]
此模組中的大多數函式都在線性時間中運作。這表示執行操作所需的時間與清單長度以相同的速率增加。例如,length/1
和 last/1
將在線性時間中執行,因為它們需要遍歷清單中的每個元素,但 first/1
將在恆定時間中執行,因為它只需要第一個元素。
清單也實作 Enumerable
協定,因此許多用於處理清單的函式都可以在 Enum
模組中找到。此外,以下函式和運算子用於清單,可以在 Kernel
中找到
Charlists
如果清單是由非負整數組成,其中每個整數代表一個 Unicode 編碼點,則該清單也可以稱為字元清單。這些整數必須
- 在範圍
0..0x10FFFF
(0..1_114_111
) 內; - 且在範圍
0xD800..0xDFFF
(55_296..57_343
) 外,此範圍在 Unicode 中保留給 UTF-16 代理對。
Elixir 使用 ~c
sigil 來定義字元清單
iex> ~c"héllo"
[104, 233, 108, 108, 111]
特別是,如果字元清單僅包含可列印的 ASCII 字元,預設會使用 ~c
sigil 將其列印回來
iex> ~c"abc"
~c"abc"
儘管表示法已變更,但原始資料仍是整數清單,可以這樣處理
iex> inspect(~c"abc", charlists: :as_list)
"[97, 98, 99]"
iex> Enum.map(~c"abc", fn num -> 1000 + num end)
[1097, 1098, 1099]
當你在 IEx 中遇到字元清單時,可以使用 IEx.Helpers.i/1
輔助函式來取得字元清單的簡要摘要,它會顯示類型、描述以及原始表示法,全部都在一個摘要中。
此行為背後的原理是為了更好地支援 Erlang 函式庫,這些函式庫可能會將文字傳回為字元清單,而不是 Elixir 字串。在 Erlang 中,字元清單是處理字串的預設方式,而在 Elixir 中則是二進制。此類函式的範例之一是 Application.loaded_applications/0
Application.loaded_applications()
#=> [
#=> {:stdlib, ~c"ERTS CXC 138 10", ~c"2.6"},
#=> {:compiler, ~c"ERTS CXC 138 10", ~c"6.0.1"},
#=> {:elixir, ~c"elixir", ~c"1.0.0"},
#=> {:kernel, ~c"ERTS CXC 138 10", ~c"4.1"},
#=> {:logger, ~c"logger", ~c"1.0.0"}
#=> ]
可以使用 ascii_printable?/2
檢查清單是否僅由可列印的 ASCII 字元組成。
不當清單絕不會被視為字元清單。
摘要
函式
檢查 list
是否為僅由可列印 ASCII 字元組成的字元清單。
從 list
中刪除指定的 element
。傳回不包含該元素的新清單。
透過移除指定 index
處的值來產生新清單。
在清單中複製指定的元素 n
次。
傳回 list
中的第一個元素,或在 list
為空時傳回 default
。
將給定的嵌套清單 list
扁平化。
將給定的嵌套清單 list
扁平化。清單 tail
將會新增至扁平化清單的尾端。
使用函式從左方摺疊(簡化)給定的清單。需要一個累加器,可以是任何值。
使用函式從右方摺疊(簡化)給定的清單。需要一個累加器,可以是任何值。
如果 list
是不適當清單,傳回 true
。否則傳回 false
。
傳回清單,其中 value
已插入指定的 index
。
接收元組清單 list
,並刪除第一個元組,其中在 position
的元素與給定的 key
相符。傳回新的清單。
接收元組清單,並傳回第一個元組,其中在元組中 position
的元素與給定的 key
相符。
接收元組清單,並傳回第一個元組,其中在元組中 position
的元素與給定的 key
相符。
接收元組清單,並傳回 true
,如果存在元組,其中在元組中 position
的元素與給定的 key
相符。
接收元組清單,如果在 position
處由 key
識別的元素存在,則用 new_tuple
取代它。
接收元組清單,並對元組中 position
的元素進行排序。
接收元組清單 list
,並用 new_tuple
取代在 position
處由 key
識別的元素。
接收元組清單 list
,並傳回第一個元組,其中在元組中 position
的元素與給定的 key
相符,以及不含已找到元組的 list
。
傳回 list
中的最後一個元素,或在 list
為空時傳回 default
。
傳回一個代表編輯腳本的關鍵字清單。
傳回一個代表編輯腳本的關鍵字清單,其中包含巢狀差異。
傳回並移除 list
中指定 index
的值。
傳回一個清單,其中指定 index
的值已被替換。
如果 list
以給定的 prefix
清單開頭,則傳回 true
;否則傳回 false
。
將字元清單轉換為原子。
將表示 Unicode 編碼點、清單或字串的整數清單轉換為字元清單。
將字元清單轉換為現有原子。
傳回文字表示為 charlist
的浮點數。
傳回文字表示為 charlist
的整數。
傳回文字表示為 charlist
的整數,其基底為 base
。
將表示編碼點、清單或字串的整數清單轉換為字串。
將清單轉換為元組。
傳回一個清單,其中指定 index
的值已更新。
如果 term
不是清單,則將其包裝在清單中。
將 list_of_lists
中每個清單的對應元素壓縮。
函式
@spec ascii_printable?(list(), 0) :: true
@spec ascii_printable?([], limit) :: true when limit: :infinity | pos_integer()
@spec ascii_printable?([...], limit) :: boolean() when limit: :infinity | pos_integer()
檢查 list
是否為僅由可列印 ASCII 字元組成的字元清單。
將一個選用的 limit
作為第二個引數。 ascii_printable?/2
僅檢查清單的可列印性,直到 limit
。
Elixir 中的可列印字元清單僅包含標準七位元 ASCII 字元編碼中的可列印字元,這些字元在十進位表示法中範圍從 32 到 126,加上下列控制字元
?\a
- 鈴聲?\b
- 退格鍵?\t
- 水平定位標籤?\n
- 換行?\v
- 垂直定位標籤?\f
- 換頁?\r
- 換行?\e
- 跳脫
如需更多資訊,請閱讀 維基百科文章 中 ASCII 標準的 字元組 區段。
範例
iex> List.ascii_printable?(~c"abc")
true
iex> List.ascii_printable?(~c"abc" ++ [0])
false
iex> List.ascii_printable?(~c"abc" ++ [0], 2)
true
不當清單不可列印,即使只由 ASCII 字元組成
iex> List.ascii_printable?(~c"abc" ++ ?d)
false
從 list
中刪除指定的 element
。傳回不包含該元素的新清單。
如果 element
在 list
中出現多次,只會移除第一次出現的項目。
範例
iex> List.delete([:a, :b, :c], :a)
[:b, :c]
iex> List.delete([:a, :b, :c], :d)
[:a, :b, :c]
iex> List.delete([:a, :b, :b, :c], :b)
[:a, :b, :c]
iex> List.delete([], :b)
[]
透過移除指定 index
處的值來產生新清單。
負數索引表示從 list
尾端開始的偏移量。如果 index
超出範圍,將傳回原始 list
。
範例
iex> List.delete_at([1, 2, 3], 0)
[2, 3]
iex> List.delete_at([1, 2, 3], 10)
[1, 2, 3]
iex> List.delete_at([1, 2, 3], -1)
[1, 2]
@spec duplicate(any(), 0) :: []
@spec duplicate(elem, pos_integer()) :: [elem, ...] when elem: var
在清單中複製指定的元素 n
次。
n
是大於或等於 0
的整數。
如果 n
是 0
,將傳回空清單。
範例
iex> List.duplicate("hello", 0)
[]
iex> List.duplicate("hi", 1)
["hi"]
iex> List.duplicate("bye", 2)
["bye", "bye"]
iex> List.duplicate([1, 2], 3)
[[1, 2], [1, 2], [1, 2]]
傳回 list
中的第一個元素,或在 list
為空時傳回 default
。
first/2
已在 Elixir v1.12.0 中引入,而 first/1
則自 v1.0.0 起可用。
範例
iex> List.first([])
nil
iex> List.first([], 1)
1
iex> List.first([1])
1
iex> List.first([1, 2, 3])
1
將給定的嵌套清單 list
扁平化。
空清單元素會被捨棄。
範例
iex> List.flatten([1, [[2], 3]])
[1, 2, 3]
iex> List.flatten([[], [[], []]])
[]
@spec flatten(deep_list, [elem]) :: [elem]
when deep_list: [elem | deep_list], elem: var
將給定的嵌套清單 list
扁平化。清單 tail
將會新增至扁平化清單的尾端。
來自 list
的空清單元素會被捨棄,但來自 tail
的空清單元素則不會。
範例
iex> List.flatten([1, [[2], 3]], [4, 5])
[1, 2, 3, 4, 5]
iex> List.flatten([1, [], 2], [3, [], 4])
[1, 2, 3, [], 4]
@spec foldl([elem], acc, (elem, acc -> acc)) :: acc when elem: var, acc: var
使用函式從左方摺疊(簡化)給定的清單。需要一個累加器,可以是任何值。
範例
iex> List.foldl([5, 5], 10, fn x, acc -> x + acc end)
20
iex> List.foldl([1, 2, 3, 4], 0, fn x, acc -> x - acc end)
2
iex> List.foldl([1, 2, 3], {0, 0}, fn x, {a1, a2} -> {a1 + x, a2 - x} end)
{6, -6}
@spec foldr([elem], acc, (elem, acc -> acc)) :: acc when elem: var, acc: var
使用函式從右方摺疊(簡化)給定的清單。需要一個累加器,可以是任何值。
範例
iex> List.foldr([1, 2, 3, 4], 0, fn x, acc -> x - acc end)
-2
iex> List.foldr([1, 2, 3, 4], %{sum: 0, product: 1}, fn x, %{sum: a1, product: a2} -> %{sum: a1 + x, product: a2 * x} end)
%{product: 24, sum: 10}
@spec improper?(maybe_improper_list()) :: boolean()
如果 list
是不適當清單,傳回 true
。否則傳回 false
。
範例
iex> List.improper?([1, 2 | 3])
true
iex> List.improper?([1, 2, 3])
false
傳回清單,其中 value
已插入指定的 index
。
請注意,index
會限制在清單長度內。負數索引表示從 list
尾端開始的偏移量。
範例
iex> List.insert_at([1, 2, 3, 4], 2, 0)
[1, 2, 0, 3, 4]
iex> List.insert_at([1, 2, 3], 10, 0)
[1, 2, 3, 0]
iex> List.insert_at([1, 2, 3], -1, 0)
[1, 2, 3, 0]
iex> List.insert_at([1, 2, 3], -10, 0)
[0, 1, 2, 3]
@spec keydelete([tuple()], any(), non_neg_integer()) :: [tuple()]
接收元組清單 list
,並刪除第一個元組,其中在 position
的元素與給定的 key
相符。傳回新的清單。
範例
iex> List.keydelete([a: 1, b: 2], :a, 0)
[b: 2]
iex> List.keydelete([a: 1, b: 2], 2, 1)
[a: 1]
iex> List.keydelete([a: 1, b: 2], :c, 0)
[a: 1, b: 2]
此函式適用於任何元組清單
iex> List.keydelete([{22, "SSH"}, {80, "HTTP"}], 80, 0)
[{22, "SSH"}]
@spec keyfind([tuple()], any(), non_neg_integer(), any()) :: any()
接收元組清單,並傳回第一個元組,其中在元組中 position
的元素與給定的 key
相符。
如果找不到符合的元組,將傳回 default
。
範例
iex> List.keyfind([a: 1, b: 2], :a, 0)
{:a, 1}
iex> List.keyfind([a: 1, b: 2], 2, 1)
{:b, 2}
iex> List.keyfind([a: 1, b: 2], :c, 0)
nil
此函式適用於任何元組清單
iex> List.keyfind([{22, "SSH"}, {80, "HTTP"}], 22, 0)
{22, "SSH"}
@spec keyfind!([tuple()], any(), non_neg_integer()) :: any()
接收元組清單,並傳回第一個元組,其中在元組中 position
的元素與給定的 key
相符。
如果找不到相符的元組,會引發錯誤。
範例
iex> List.keyfind!([a: 1, b: 2], :a, 0)
{:a, 1}
iex> List.keyfind!([a: 1, b: 2], 2, 1)
{:b, 2}
iex> List.keyfind!([a: 1, b: 2], :c, 0)
** (KeyError) key :c at position 0 not found in: [a: 1, b: 2]
此函式適用於任何元組清單
iex> List.keyfind!([{22, "SSH"}, {80, "HTTP"}], 22, 0)
{22, "SSH"}
@spec keymember?([tuple()], any(), non_neg_integer()) :: boolean()
接收元組清單,並傳回 true
,如果存在元組,其中在元組中 position
的元素與給定的 key
相符。
範例
iex> List.keymember?([a: 1, b: 2], :a, 0)
true
iex> List.keymember?([a: 1, b: 2], 2, 1)
true
iex> List.keymember?([a: 1, b: 2], :c, 0)
false
此函式適用於任何元組清單
iex> List.keymember?([{22, "SSH"}, {80, "HTTP"}], 22, 0)
true
@spec keyreplace([tuple()], any(), non_neg_integer(), tuple()) :: [tuple()]
接收元組清單,如果在 position
處由 key
識別的元素存在,則用 new_tuple
取代它。
範例
iex> List.keyreplace([a: 1, b: 2], :a, 0, {:a, 3})
[a: 3, b: 2]
iex> List.keyreplace([a: 1, b: 2], :a, 1, {:a, 3})
[a: 1, b: 2]
此函式適用於任何元組清單
iex> List.keyreplace([{22, "SSH"}, {80, "HTTP"}], 22, 0, {22, "Secure Shell"})
[{22, "Secure Shell"}, {80, "HTTP"}]
@spec keysort( [tuple()], non_neg_integer(), (any(), any() -> boolean()) | :asc | :desc | module() | {:asc | :desc, module()} ) :: [tuple()]
接收元組清單,並對元組中 position
的元素進行排序。
排序是穩定的。
自 Elixir v1.14.0 起,提供 sorter
參數。類似於 Enum.sort/2
,排序器可以是匿名函式、原子 :asc
或 :desc
,或實作比較函式的模組。
範例
iex> List.keysort([a: 5, b: 1, c: 3], 1)
[b: 1, c: 3, a: 5]
iex> List.keysort([a: 5, c: 1, b: 3], 0)
[a: 5, b: 3, c: 1]
以遞減順序排序
iex> List.keysort([a: 5, c: 1, b: 3], 0, :desc)
[c: 1, b: 3, a: 5]
與 Enum.sort/2
相同,避免使用預設的排序函式來排序結構,因為預設會執行結構比較,而非語意比較。在這種情況下,您應該傳遞排序函式作為第三個元素,或任何實作 compare/2
函式的模組。例如,如果您有包含使用者名稱和生日的元組,而且您想要依據生日排序,以遞增和遞減順序,您應該執行
iex> users = [
...> {"Ellis", ~D[1943-05-11]},
...> {"Lovelace", ~D[1815-12-10]},
...> {"Turing", ~D[1912-06-23]}
...> ]
iex> List.keysort(users, 1, Date)
[
{"Lovelace", ~D[1815-12-10]},
{"Turing", ~D[1912-06-23]},
{"Ellis", ~D[1943-05-11]}
]
iex> List.keysort(users, 1, {:desc, Date})
[
{"Ellis", ~D[1943-05-11]},
{"Turing", ~D[1912-06-23]},
{"Lovelace", ~D[1815-12-10]}
]
@spec keystore([tuple()], any(), non_neg_integer(), tuple()) :: [tuple(), ...]
接收元組清單 list
,並用 new_tuple
取代在 position
處由 key
識別的元素。
如果元素不存在,會將其新增至 list
的結尾。
範例
iex> List.keystore([a: 1, b: 2], :a, 0, {:a, 3})
[a: 3, b: 2]
iex> List.keystore([a: 1, b: 2], :c, 0, {:c, 3})
[a: 1, b: 2, c: 3]
此函式適用於任何元組清單
iex> List.keystore([{22, "SSH"}], 80, 0, {80, "HTTP"})
[{22, "SSH"}, {80, "HTTP"}]
@spec keytake([tuple()], any(), non_neg_integer()) :: {tuple(), [tuple()]} | nil
接收元組清單 list
,並傳回第一個元組,其中在元組中 position
的元素與給定的 key
相符,以及不含已找到元組的 list
。
如果找不到此類元組,會傳回 nil
。
範例
iex> List.keytake([a: 1, b: 2], :a, 0)
{{:a, 1}, [b: 2]}
iex> List.keytake([a: 1, b: 2], 2, 1)
{{:b, 2}, [a: 1]}
iex> List.keytake([a: 1, b: 2], :c, 0)
nil
此函式適用於任何元組清單
iex> List.keytake([{22, "SSH"}, {80, "HTTP"}], 80, 0)
{{80, "HTTP"}, [{22, "SSH"}]}
傳回 list
中的最後一個元素,或在 list
為空時傳回 default
。
last/2
已在 Elixir v1.12.0 中引入,而 last/1
已自 v1.0.0 起提供。
範例
iex> List.last([])
nil
iex> List.last([], 1)
1
iex> List.last([1])
1
iex> List.last([1, 2, 3])
3
傳回一個代表編輯腳本的關鍵字清單。
演算法概述於 E. Myers 的「一個 O(ND) 差異演算法及其變體」論文中。
編輯指令碼 是關鍵字清單。每個關鍵字描述為了讓 list1
更接近於等於 list2
所採取的「編輯動作」;關鍵字可以是 :eq
、:ins
或 :del
。每個值都是 list1
或 list2
的子清單,應該在 list1
中插入(如果對應的關鍵字是 :ins
)、刪除(如果對應的關鍵字是 :del
)或保持不變(如果對應的關鍵字是 :eq
),才能更接近於 list2
。
如果您想在 diff 指令碼中處理巢狀,請參閱 myers_difference/3
。
範例
iex> List.myers_difference([1, 4, 2, 3], [1, 2, 3, 4])
[eq: [1], del: [4], eq: [2, 3], ins: [4]]
@spec myers_difference(list(), list(), (term(), term() -> script | nil)) :: script when script: [{:eq | :ins | :del | :diff, list()}]
傳回一個代表編輯腳本的關鍵字清單,其中包含巢狀差異。
這是 myers_difference/2
的延伸,其中如果要計算巢狀差異,可以提供 diff_script
函式。函式可以傳回清單,其中包含內部編輯指令碼,或者如果沒有此類指令碼,則傳回 nil
。傳回的內部編輯指令碼將位於 :diff
關鍵字之下。
範例
iex> List.myers_difference(["a", "db", "c"], ["a", "bc"], &String.myers_difference/2)
[eq: ["a"], diff: [del: "d", eq: "b", ins: "c"], del: ["c"]]
傳回並移除 list
中指定 index
的值。
負數索引表示從 list
尾端開始的偏移量。如果 index
超出範圍,將傳回原始 list
。
範例
iex> List.pop_at([1, 2, 3], 0)
{1, [2, 3]}
iex> List.pop_at([1, 2, 3], 5)
{nil, [1, 2, 3]}
iex> List.pop_at([1, 2, 3], 5, 10)
{10, [1, 2, 3]}
iex> List.pop_at([1, 2, 3], -1)
{3, [1, 2]}
傳回一個清單,其中指定 index
的值已被替換。
負數索引表示從 list
尾端開始的偏移量。如果 index
超出範圍,將傳回原始 list
。
範例
iex> List.replace_at([1, 2, 3], 0, 0)
[0, 2, 3]
iex> List.replace_at([1, 2, 3], 10, 0)
[1, 2, 3]
iex> List.replace_at([1, 2, 3], -1, 0)
[1, 2, 0]
iex> List.replace_at([1, 2, 3], -10, 0)
[1, 2, 3]
@spec starts_with?([...], [...]) :: boolean()
@spec starts_with?(list(), []) :: true
@spec starts_with?([], [...]) :: false
如果 list
以給定的 prefix
清單開頭,則傳回 true
;否則傳回 false
。
如果 prefix
是空清單,它會傳回 true
。
範例
iex> List.starts_with?([1, 2, 3], [1, 2])
true
iex> List.starts_with?([1, 2], [1, 2, 3])
false
iex> List.starts_with?([:alpha], [])
true
iex> List.starts_with?([], [:alpha])
false
將字元清單轉換為原子。
Elixir 支援從包含任何 Unicode 碼點的字元清單進行轉換。
由編譯器內嵌。
範例
iex> List.to_atom(~c"Elixir")
:Elixir
iex> List.to_atom(~c"🌢 Elixir")
:"🌢 Elixir"
@spec to_charlist(:unicode.charlist()) :: charlist()
將表示 Unicode 編碼點、清單或字串的整數清單轉換為字元清單。
請注意,此函式預期清單中的整數代表 Unicode 碼點。如果您有位元組清單,則必須改用 :binary
模組。
範例
iex> ~c"æß" = List.to_charlist([0x00E6, 0x00DF])
[230, 223]
iex> List.to_charlist([0x0061, "bc"])
~c"abc"
iex> List.to_charlist([0x0064, "ee", [~c"p"]])
~c"deep"
將字元清單轉換為現有原子。
Elixir 支援從包含任何 Unicode 碼點的字元清單進行轉換。如果原子不存在,則會引發 ArgumentError
。
由編譯器內嵌。
原子和模組
由於 Elixir 是一種編譯語言,模組中定義的原子只會在載入該模組後才會存在,這通常會在執行模組中的函數時發生。因此,通常建議僅呼叫
List.to_existing_atom/1
來轉換在呼叫to_existing_atom/1
函數的模組中定義的原子。
範例
iex> _ = :my_atom
iex> List.to_existing_atom(~c"my_atom")
:my_atom
iex> _ = :"🌢 Elixir"
iex> List.to_existing_atom(~c"🌢 Elixir")
:"🌢 Elixir"
傳回文字表示為 charlist
的浮點數。
由編譯器內嵌。
範例
iex> List.to_float(~c"2.2017764e+0")
2.2017764
傳回文字表示為 charlist
的整數。
由編譯器內嵌。
範例
iex> List.to_integer(~c"123")
123
傳回文字表示為 charlist
的整數,其基底為 base
。
由編譯器內嵌。
基底必須介於 2
和 36
之間。
範例
iex> List.to_integer(~c"3FF", 16)
1023
@spec to_string(:unicode.charlist()) :: String.t()
將表示編碼點、清單或字串的整數清單轉換為字串。
要轉換為字串,清單必須為空或僅包含下列元素
- 字串
- 代表 Unicode 編碼點的整數
- 包含這三種元素之一的清單
請注意,此函式預期清單中的整數代表 Unicode 碼點。如果您有位元組清單,則必須改用 :binary
模組。
範例
iex> List.to_string([0x00E6, 0x00DF])
"æß"
iex> List.to_string([0x0061, "bc"])
"abc"
iex> List.to_string([0x0064, "ee", [~c"p"]])
"deep"
iex> List.to_string([])
""
將清單轉換為元組。
由編譯器內嵌。
範例
iex> List.to_tuple([:share, [:elixir, 163]])
{:share, [:elixir, 163]}
傳回一個清單,其中指定 index
的值已更新。
負數索引表示從 list
尾端開始的偏移量。如果 index
超出範圍,將傳回原始 list
。
範例
iex> List.update_at([1, 2, 3], 0, &(&1 + 10))
[11, 2, 3]
iex> List.update_at([1, 2, 3], 10, &(&1 + 10))
[1, 2, 3]
iex> List.update_at([1, 2, 3], -1, &(&1 + 10))
[1, 2, 13]
iex> List.update_at([1, 2, 3], -10, &(&1 + 10))
[1, 2, 3]
@spec wrap(term()) :: maybe_improper_list()
如果 term
不是清單,則將其包裝在清單中。
如果 term
已是清單,它會傳回清單。如果 term
是 nil
,它會傳回空清單。
範例
iex> List.wrap("hello")
["hello"]
iex> List.wrap([1, 2, 3])
[1, 2, 3]
iex> List.wrap(nil)
[]
將 list_of_lists
中每個清單的對應元素壓縮。
只要任何清單終止,拉鍊就會完成。
範例
iex> List.zip([[1, 2], [3, 4], [5, 6]])
[{1, 3, 5}, {2, 4, 6}]
iex> List.zip([[1, 2], [3], [5, 6]])
[{1, 3, 5}]