- (商品の値段が入った)配列を定義する
-
fetch: コレクション内のN番目の要素を取得(取得できない場合)
-
filter: コレクション内で特定の条件を満たす要素のみ抽出する�
-
any: コレクション内の要素のいずれかが〜である場合trueを返す
cd elixir-ex-cli-sample/
iex -S mix
# 本の名前, カテゴリを持つ配列を定義
book_samples_1 = [
%{:name => "Elixir handbook",:category => "programming"},
%{:name => "Erlang handbook",:category => "programming"},
%{:name => "Detective Poirot",:category => "novel"}
]
{:ok, first} = Enum.fetch(book_samples_1, 0) # 先頭の要素を取得する
is_map(first) # 取得結果(first)がMap型であることを確認
IO.puts(first.name) # 取得結果(first)から本の名前が得られることが確認できます
pos = length(book_samples_1) - 1
{:ok, last} = Enum.fetch(book_samples_1, pos)
IO.puts(last.name)
pos = div(length(book_samples_1) - 1, 2)
result = Enum.fetch(book_samples_1, pos)
is_tuple(result)
elem(result, 0)
elem(result, 1)
IO.puts(middle.name)
pos = 10
result = Enum.fetch(book_samples_1, pos)
IO.inspect(result)
if result === :error, do: IO.puts("エラー: リスト範囲外の要素にアクセスしました")
pos = -1
result = Enum.fetch(book_samples_1, pos)
if result === :error, do: IO.puts("エラー: リスト範囲外の要素にアクセスしました")
- (本の名前, カテゴリ, 値段, セールフラグ)のリストを定義する
book_samples_2 = [
%{
:name => "Elixir handbook",
:category => "programming",
:price => 4980,
:sale => true
},
%{
:name => "Erlang handbook",
:category => "programming",
:price => 4980,
:sale => false
},
%{
:name => "Detective Poirot",
:category => "novel",
:price => 4980,
:sale => false
}
]
exists_sale = Enum.any?(book_samples_2, fn book -> book.sale === true end)
IO.puts(exists_sale)
- (本の名前,カテゴリ,値段,著者,セールフラグ)のリストを定義する
book_samples_3 = [
%{
:name => "Elixir handbook",
:category => "programming",
:price => 4980,
:sale => true,
:author => "Sir Elixir"
},
%{
:name => "Erlang handbook",
:category => "programming",
:price => 4980,
:sale => false,
:author => "Sir Elixir"
},
%{
:name => "Detective Poirot",
:category => "novel",
:price => 4980,
:sale => false,
:author => "Poirot"
}
]
- 本の著者が 'Sir Elixir' かつ セール中の本だけを抽出する
filtered_books = Enum.filter(book_samples_3, fn book -> book.sale === true and book.author === "Sir Elixir" end)
Enum.each(filtered_books, fn fb -> IO.puts(fb.name) end)