1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

概要

paiza.ioでelixirやってみた。
Map使ってみた。

サンプルコード

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)
#Map.from_struct(%User{name: "john"})
|> IO.inspect
Map.get(%{a: 1}, :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


実行結果

%{b: 2}
%{a: 1, c: 3}
true
{:ok, 1}
1
%{name: nil}
1
{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]

成果物

以上。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?