リストに変換
> Enum.to_list 1..5
[1, 2, 3, 4, 5]
> Enum.to_list %{one: 1, two: 2, three: 3}
[one: 1, three: 3, two: 2]
> Enum.to_list {1, 2, 3}
** (Protocol.UndefinedError) protocol Enumerable not implemented for {1, 2, 3}
(elixir) lib/enum.ex:1: Enumerable.impl_for!/1
(elixir) lib/enum.ex:141: Enumerable.reduce/3
(elixir) lib/enum.ex:1919: Enum.reverse/1
(elixir) lib/enum.ex:2588: Enum.to_list/1
> Enum.to_list [a: "a", b: "b", c: "c"]
[a: "a", b: "b", c: "c"]結合
> Enum.concat([1, 2, 3], [4, 5, 6])
[1, 2, 3, 4, 5, 6]
> Enum.concat([1, 2, 3], 'abc')
[1, 2, 3, 97, 98, 99]
> Enum.concat([1, 2, 3], %{four: 4, five: 5})
[1, 2, 3, {:five, 5}, {:four, 4}]
> Enum.concat(%{one: 1, two: 2}, %{three: 3, four: 4})
[one: 1, two: 2, four: 4, three: 3]
> Enum.concat({1, 2, 3}, {4, 5, 6})
** (Protocol.UndefinedError) protocol Enumerable not implemented for {1, 2, 3}
(elixir) lib/enum.ex:1: Enumerable.impl_for!/1
(elixir) lib/enum.ex:141: Enumerable.reduce/3
(elixir) lib/enum.ex:1919: anonymous fn/3 in Enum.concat/1
(elixir) lib/enum.ex:1899: Enum."-concat/1-lists^foldl/2-1-"/3
(elixir) lib/enum.ex:1899: Enum.concat/1
> Enum.concat({:one, 1}, {:two, 2})
** (Protocol.UndefinedError) protocol Enumerable not implemented for {:one, 1}
(elixir) lib/enum.ex:1: Enumerable.impl_for!/1
(elixir) lib/enum.ex:141: Enumerable.reduce/3
(elixir) lib/enum.ex:1919: anonymous fn/3 in Enum.concat/1
(elixir) lib/enum.ex:1899: Enum."-concat/1-lists^foldl/2-1-"/3
(elixir) lib/enum.ex:1899: Enum.concat/1
> Enum.concat([one: 1], [two: 2])
[one: 1, two: 2]「各要素に関数を適用した結果」を要素とするコレクション生成
> Enum.map(list, &(&1*10))
[10, 20, 30, 40, 50]
> Enum.map(list, &String.duplicate("*", &1))
["*", "**", "***", "****", "*****"]位置、または条件によって要素を選択
> Enum.at(10..20, 3)
13
> Enum.at(10..20, 20)
nil
> Enum.at(10..20, 20, :no_one_here)
:no_one_here
> Enum.filter(list, &(&1 > 2))
[3, 4, 5]
st, &Integer.is_even/1
[2, 4]
> Enum.reject list, &Integer.is_even/1
[1, 3, 5]
> Enum.reject list, &Integer.is_even(&1)
[1, 3, 5]ソート、比較
ort ["there", "was", "a", "crooked", "man"]
["a", "crooked", "man", "there", "was"]
> Enum.sort ["there", "was", "a", "crooked", "man"],
> &(String.length(&1) <= String.length(&2))
["a", "was", "man", "there", "crooked"]
> Enum.sort ["there", "was", "a", "crooked", "man"],
> &(String.length(&2) <= String.length(&1))
["crooked", "there", "was", "man", "a"]
> Enum.max ["there", "was", "a", "crooked", "man"]
"was"
> Enum.max_by ["there", "was", "a", "crooked", "man"],
> &String.length/1
"crooked"分割
> Enum.take list, 3
[1, 2, 3]
> Enum.take_every list, 2
[1, 3, 5]
> Enum.take_while list, &(&1 < 4)
[1, 2, 3]
> Enum.split list, 3
{[1, 2, 3], [4, 5]}
> Enum.split_while list, &(&1 < 4)
{[1, 2, 3], [4, 5]}要素を連結
> Enum.join list
"12345"
> Enum.join list, ", "
"1, 2, 3, 4, 5"述語演算
> Enum.all? list, &(&1 < 4)
false
> Enum.any? list, &(&1 < 4)
true
> Enum.member? list, 4
true
> Enum.empty? list
falseマージ
> Enum.zip list, [:a, :b, :c]
[{1, :a}, {2, :b}, {3, :c}]
> Enum.zip list, list
[{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}]
> Enum.zip list, [:a, :b]
[{1, :a}, {2, :b}]
> Enum.with_index ["once", "upon", "a", "time"]
[{"once", 0}, {"upon", 1}, {"a", 2}, {"time", 3}]1つの値にたたみ込み
> Enum.reduce(1..100, &(&1 + &2))
5050
> Enum.reduce(["now", "is", "the", "time"], fn word, longest ->
> if String.length(word) > String.length(longest) do
> word
> else
> longest
> end
> end)
"time"
> Enum.reduce(["now", "is", "the", "time"], 0, fn word, longest ->
> if String.length(word) > longest,
> do: String.length(word),
> else: longest
> end)
4手札の配布
> import Enum
Enum
> deck = for rank <- '23456789TJQKA', suit <- 'CDHS', do: [suit, rank]
['C2', 'D2', 'H2', 'S2', 'C3', 'D3', 'H3', 'S3', 'C4', 'D4', 'H4', 'S4', 'C5',
'D5', 'H5', 'S5', 'C6', 'D6', 'H6', 'S6', 'C7', 'D7', 'H7', 'S7', 'C8', 'D8',
'H8', 'S8', 'C9', 'D9', 'H9', 'S9', 'CT', 'DT', 'HT', 'ST', 'CJ', 'DJ', 'HJ',
'SJ', 'CQ', 'DQ', 'HQ', 'SQ', 'CK', 'DK', 'HK', 'SK', 'CA', 'DA', ...]
> deck |> shuffle |> take(13)
['D8', 'S7', 'S6', 'D4', 'S8', 'C4', 'S5', 'HK', 'SQ', 'SK', 'S4', 'C3', 'H9']
> hands = deck |> shuffle |> chunk(13)
[
['ST', 'S2', 'D9', 'C8', 'D2', 'HA', 'S4', 'H9', 'SA', 'DJ', 'D6', 'HJ', 'HK'],
['DA', 'C2', 'S7', 'C5', 'S5', 'H5', 'C7', 'S6', 'H3', 'CT', 'C9', 'DT', 'D7'],
['C4', 'S9', 'CJ', 'HQ', 'CK', 'H6', 'DQ', 'SJ', 'H8', 'CA', 'H7', 'C6', 'D5'],
['D4', 'H4', 'SK', 'C3', 'DK', 'H2', 'CQ', 'SQ', 'D3', 'HT', 'D8', 'S3', 'S8']
]ソートについての注意点
Enum.sort/2 に渡す関数は、同値の場合にtrueを返すようにすること
そうでないと、同値にもかかわらず、順序が入れ替わってしまう場合がある
https://hexdocs.pm/elixir/Enum.html#sort/2
> Enum.sort ["some", "kind", "of", "monster"], &(byte_size(&1) <= byte_size(&2))
["of", "some", "kind", "monster"]
> Enum.sort ["some", "kind", "of", "monster"], &(byte_size(&1) < byte_size(&2))
["of", "kind", "some", "monster"]
>