paiza.ioでelixir その169

# 概要
paiza.ioでelixirやってみた。
access使ってみた。

# サンプルコード

```

keywords = [a: 1, b: 2]
keywords[:a]
|> IO.inspect
keywords[:c]
|> IO.inspect
map = %{a: 1, b: 2}
map[:a]
|> IO.inspect
star_ratings = %{1.0 => "★", 1.5 => "★☆", 2.0 => "★★"}
star_ratings[1.5]
|> IO.inspect
keywords = [a: 1, b: 2]
keywords[:c][:unknown]
|> IO.inspect
nil[:a]
|> IO.inspect
users = %{"john" => %{age: 27}, "meg" => %{age: 23}}
put_in(users["john"][:age], 28)
|> IO.inspect
users = %{"john" => %{age: 27}, "meg" => %{age: 23}}
put_in(users["john"].age, 28)
|> IO.inspect
languages = [
  %{name: "elixir", type: :functional},
  %{name: "c", type: :procedural}
]
user = %{name: "john", languages: languages}
update_in(user, [:languages, Access.all(), :name], &String.upcase/1)
|> IO.inspect
list = [%{name: "john"}, %{name: "mary"}]
get_in(list, [Access.all(), :name])
|> IO.inspect
get_and_update_in(list, [Access.all(), :name], fn prev ->
  {prev, String.upcase(prev)}
end)
|> IO.inspect
pop_in(list, [Access.all(), :name])
|> IO.inspect
require Integer
get_and_update_in([1, 2, 3, 4, 5], [Access.all()], fn num ->
  if Integer.is_even(num), do: :pop, else: {num, num * 2}
end)
|> IO.inspect
#get_in(%{}, [Access.all()])
#|> IO.inspect
#at!(index)(since 1.11.0)
#|> IO.inspect
get_in([:a, :b, :c], [Access.at!(2)])
|> IO.inspect
#get_in([:a, :b, :c], [Access.at!(3)])
#|> IO.inspect
list = [%{name: "john"}, %{name: "mary"}]
get_in(list, [Access.at(1), :name])
|> IO.inspect
get_in(list, [Access.at(-1), :name])
|> IO.inspect
get_and_update_in(list, [Access.at(0), :name], fn prev ->
  {prev, String.upcase(prev)}
end)
|> IO.inspect
get_and_update_in(list, [Access.at(-1), :name], fn prev ->
  {prev, String.upcase(prev)}
end)
|> IO.inspect
list = [%{name: "john"}, %{name: "mary"}]
pop_in(list, [Access.at(0)])
|> IO.inspect
pop_in(list, [Access.at(0), :name])
|> IO.inspect
list = [%{name: "john"}, %{name: "mary"}]
get_in(list, [Access.at(10), :name])
|> IO.inspect
get_and_update_in(list, [Access.at(10), :name], fn prev ->
  {prev, String.upcase(prev)}
end)
|> IO.inspect
#get_in(%{}, [Access.at(1)])
#|> IO.inspect
map = %{user: {"john", 27}}
get_in(map, [:user, Access.elem(0)])
|> IO.inspect
get_and_update_in(map, [:user, Access.elem(0)], fn prev ->
  {prev, String.upcase(prev)}
end)
|> IO.inspect
#pop_in(map, [:user, Access.elem(0)])
#|> IO.inspect
#get_in(%{}, [Access.elem(0)])
#|> IO.inspect
Access.fetch!(%{name: "meg", age: 26}, :name)
|> IO.inspect
Access.fetch(%{name: "meg", age: 26}, :name)
|> IO.inspect
list = [%{name: "john", salary: 10}, %{name: "francine", salary: 30}]
get_in(list, [Access.filter(&(&1.salary > 20)), :name])
|> IO.inspect
get_and_update_in(list, [Access.filter(&(&1.salary <= 20)), :name], fn prev ->
  {prev, String.upcase(prev)}
end)
|> IO.inspect
list = [%{name: "john", salary: 10}, %{name: "francine", salary: 30}]
pop_in(list, [Access.filter(&(&1.salary >= 20))])
|> IO.inspect
pop_in(list, [Access.filter(&(&1.salary >= 20)), :name])
|> IO.inspect
list = [%{name: "john", salary: 10}, %{name: "francine", salary: 30}]
get_in(list, [Access.filter(&(&1.salary >= 50)), :name])
|> IO.inspect
get_and_update_in(list, [Access.filter(&(&1.salary >= 50)), :name], fn prev ->
  {prev, String.upcase(prev)}
end)
|> IO.inspect
#get_in([], [Access.filter(5)])
#|> IO.inspect
#get_in(%{}, [Access.filter(fn a -> a == 10 end)])
#|> IO.inspect
Access.get(%{name: "john"}, :name, "default name")
|> IO.inspect
Access.get(%{name: "john"}, :age, 25)
|> IO.inspect
Access.get([ordered: true], :timeout)
|> IO.inspect
Access.get_and_update([a: 1], :a, fn current_value ->
  {current_value, current_value + 1}
end)
|> IO.inspect
map = %{user: %{name: "john"}}
get_in(map, [Access.key!(:user), Access.key!(:name)])
|> IO.inspect
get_and_update_in(map, [Access.key!(:user), Access.key!(:name)], fn prev ->
  {prev, String.upcase(prev)}
end)
|> IO.inspect
pop_in(map, [Access.key!(:user), Access.key!(:name)])
|> IO.inspect
#get_in(map, [Access.key!(:user), Access.key!(:unknown)])
#|> IO.inspect
#get_in([], [Access.key!(:foo)])
#|> IO.inspect
get_in(%{}, [Access.key(:user, %{}), Access.key(:name, "meg")])
|> IO.inspect
put_in(%{}, [Access.key(:user, %{}), Access.key(:name)], "Mary")
|> IO.inspect
map = %{user: %{name: "john"}}
get_in(map, [Access.key(:unknown, %{}), Access.key(:name, "john")])
|> IO.inspect
get_and_update_in(map, [Access.key(:user), Access.key(:name)], fn prev ->
  {prev, String.upcase(prev)}
end)
|> IO.inspect
pop_in(map, [Access.key(:user), Access.key(:name)])
|> IO.inspect
#get_in([], [Access.key(:foo)])
#|> IO.inspect
Access.pop(%{name: "Elixir", creator: "Valim"}, :name)
|> IO.inspect
Access.pop([name: "Elixir", creator: "Valim"], :name)
|> IO.inspect
Access.pop(%{name: "Elixir", creator: "Valim"}, :year)
|> IO.inspect



```

# 実行結果

```
1
nil
1
"★☆"
nil
nil
%{"john" => %{age: 28}, "meg" => %{age: 23}}
%{"john" => %{age: 28}, "meg" => %{age: 23}}
%{
  languages: [
    %{name: "ELIXIR", type: :functional},
    %{name: "C", type: :procedural}
  ],
  name: "john"
}
["john", "mary"]
{["john", "mary"], [%{name: "JOHN"}, %{name: "MARY"}]}
{["john", "mary"], [%{}, %{}]}
{[1, 2, 3, 4, 5], [2, 6, 10]}
:c
"mary"
"mary"
{"john", [%{name: "JOHN"}, %{name: "mary"}]}
{"mary", [%{name: "john"}, %{name: "MARY"}]}
{%{name: "john"}, [%{name: "mary"}]}
{"john", [%{}, %{name: "mary"}]}
nil
{nil, [%{name: "john"}, %{name: "mary"}]}
"john"
{"john", %{user: {"JOHN", 27}}}
"meg"
{:ok, "meg"}
["francine"]
{["john"], [%{name: "JOHN", salary: 10}, %{name: "francine", salary: 30}]}
{[%{name: "francine", salary: 30}], [%{name: "john", salary: 10}]}
{["francine"], [%{name: "john", salary: 10}, %{salary: 30}]}
[]
{[], [%{name: "john", salary: 10}, %{name: "francine", salary: 30}]}
"john"
25
nil
{1, [a: 2]}
"john"
{"john", %{user: %{name: "JOHN"}}}
{"john", %{user: %{}}}
"meg"
%{user: %{name: "Mary"}}
"john"
{"john", %{user: %{name: "JOHN"}}}
{"john", %{user: %{}}}
{"Elixir", %{creator: "Valim"}}
{"Elixir", [creator: "Valim"]}
{nil, %{creator: "Valim", name: "Elixir"}}
```

# 成果物

https://paiza.io/projects/8RjvGNMZbbp_nmCtQrKq0A?language=php


以上。