概要
paiza.ioでelixirやってみた。
Map使ってみた。
サンプルコード
%{}
|> IO.inspect
%{"one" => :two, 3 => "four"}
|> IO.inspect
%{3 => "four", "one" => :two}
|> IO.inspect
%{a: 1, b: 2}
|> IO.inspect
%{"hello" => "world", a: 1, b: 2}
|> IO.inspect
%{:a => 1, :b => 2, "hello" => "world"}
|> IO.inspect
map = %{a: 1, b: 2}
|> IO.inspect
Map.fetch(map, :a)
|> IO.inspect
map[:b]
|> IO.inspect
map["non_existing_key"]
|> IO.inspect
map = %{foo: "bar", baz: "bong"}
|> IO.inspect
map.foo
|> IO.inspect
#map.non_existing_key
%{} = %{foo: "bar"}
|> IO.inspect
%{foo: "bar"}
|> IO.inspect
%{a: a} = %{:a => 1, "b" => 2, [:c, :e, :e] => 3}
|> IO.inspect
n = 1
|> IO.inspect
%{n => :one}
|> IO.inspect
%{1 => :one}
|> IO.inspect
%{^n => :one} = %{1 => :one, 2 => :two, 3 => :three}
|> IO.inspect
%{1 => :one, 2 => :two, 3 => :three}
|> IO.inspect
map = %{one: 1, two: 2}
|> IO.inspect
%{map | one: "one"}
|> IO.inspect
Map.delete(%{a: 1, b: 2}, :a)
|> IO.inspect
Map.drop(%{a: 1, b: 2, c: 3}, [:b, :d])
|> IO.inspect
Map.equal?(%{a: 1, b: 2}, %{b: 2, a: 1})
|> IO.inspect
Map.fetch(%{a: 1}, :a)
|> IO.inspect
Map.fetch!(%{a: 1}, :a)
|> IO.inspect
defmodule User do
defstruct [:name]
end
Map.from_struct(User)
|> IO.inspect
#Map.from_struct(%User{name: "john"})
Map.get(%{}, :a)
|> IO.inspect
Map.get_and_update(%{a: 1}, :a, fn current_value ->
{current_value, "new value!"}
end)
|> IO.inspect
Map.get_and_update!(%{a: 1}, :a, fn current_value ->
{current_value, "new value!"}
end)
|> IO.inspect
map = %{a: 1}
fun = fn ->
13
end
Map.get_lazy(map, :a, fun)
|> IO.inspect
Map.has_key?(%{a: 1}, :a)
|> IO.inspect
Map.keys(%{a: 1, b: 2})
|> IO.inspect
Map.merge(%{a: 1, b: 2}, %{a: 3, d: 4})
|> IO.inspect
Map.merge(%{a: 1, b: 2}, %{a: 3, d: 4}, fn _k, v1, v2 ->
v1 + v2
end)
|> IO.inspect
Map.new()
|> IO.inspect
Map.new([{:b, 1}, {:a, 2}])
|> IO.inspect
Map.new([:a, :b], fn x ->
{x, x}
end)
|> IO.inspect
Map.pop(%{a: 1}, :a)
|> IO.inspect
Map.pop!(%{a: 1}, :a)
|> IO.inspect
map = %{a: 1}
fun = fn ->
13
end
Map.pop_lazy(map, :a, fun)
|> IO.inspect
Map.put(%{a: 1}, :b, 2)
|> IO.inspect
Map.put_new(%{a: 1}, :b, 2)
|> IO.inspect
map = %{a: 1}
fun = fn ->
3
end
Map.put_new_lazy(map, :a, fun)
|> IO.inspect
Map.replace(%{a: 1, b: 2}, :a, 3)
|> IO.inspect
Map.replace!(%{a: 1, b: 2}, :a, 3)
|> IO.inspect
Map.split(%{a: 1, b: 2, c: 3}, [:a, :c, :e])
|> IO.inspect
Map.take(%{a: 1, b: 2, c: 3}, [:a, :c, :e])
|> IO.inspect
Map.to_list(%{a: 1})
|> IO.inspect
Map.update(%{a: 1}, :a, 13, fn existing_value ->
existing_value * 2
end)
|> IO.inspect
Map.update!(%{a: 1}, :a, &(&1 * 2))
|> IO.inspect
Map.values(%{a: 1, b: 2})
|> IO.inspect
実行結果
%{}
%{3 => "four", "one" => :two}
%{3 => "four", "one" => :two}
%{a: 1, b: 2}
%{:a => 1, :b => 2, "hello" => "world"}
%{:a => 1, :b => 2, "hello" => "world"}
%{a: 1, b: 2}
{:ok, 1}
2
nil
%{baz: "bong", foo: "bar"}
"bar"
%{foo: "bar"}
%{foo: "bar"}
%{:a => 1, [:c, :e, :e] => 3, "b" => 2}
1
%{1 => :one}
%{1 => :one}
%{1 => :one, 2 => :two, 3 => :three}
%{1 => :one, 2 => :two, 3 => :three}
%{one: 1, two: 2}
%{one: "one", two: 2}
%{b: 2}
%{a: 1, c: 3}
true
{:ok, 1}
1
%{name: nil}
nil
{1, %{a: "new value!"}}
{1, %{a: "new value!"}}
1
true
[:a, :b]
%{a: 3, b: 2, d: 4}
%{a: 4, b: 2, d: 4}
%{}
%{a: 2, b: 1}
%{a: :a, b: :b}
{1, %{}}
{1, %{}}
{1, %{}}
%{a: 1, b: 2}
%{a: 1, b: 2}
%{a: 1}
%{a: 3, b: 2}
%{a: 3, b: 2}
{%{a: 1, c: 3}, %{b: 2}}
%{a: 1, c: 3}
[a: 1]
%{a: 2}
%{a: 2}
[1, 2]
×
成果物
以上。