paiza.ioでelixir その161

# 概要
paiza.ioでelixirやってみた。
map, structの変換やってみた。

# 参考にしたページ。

https://qiita.com/piacerex/items/14633be1462b391421d8

# サンプルコード

```

#map → struct
struct(Range, %{first: 1, last: 10, step: 2})
|> IO.inspect
struct(Range, %{})
|> IO.inspect
#struct → map
%Range{first: 1, last: 10, step: 2} 
|> Map.from_struct
|> IO.inspect
#Map replace/add key-value
%{first: 1, last: 10, step: 2} 
|> Map.put(:first, 2)
|> IO.inspect
#Map replace value only
%{first: 1, last: 10, step: 2} 
|> Map.update!(:first, & &1 * 3)
|> IO.inspect
#String-key map → Atom-key map
#方法その1
%{"first" => 1, "last" => 10, "step" => 2} 
|> Map.new(fn {k, v} -> 
    {String.to_atom(k), v} 
end)
|> IO.inspect
%{"first" => 1, "last" => 10, "step" => 2} 
|> Map.new(&{String.to_atom(elem(&1, 0)), elem(&1, 1)})
|> IO.inspect
#方法その2
for {k, v} <- %{"first" => 1, "last" => 10, "step" => 2}, into: %{}, do: {String.to_atom(k), v}
|> IO.inspect
#方法その3
#%{"first" => 1, "last" => 10, "step" => 2} 
#|> Jason.encode! 
#|> Jason.decode!(keys: :atoms)
#|> IO.inspect
#Atom-key map → String-key map
#方法その1
%{first: 1, last: 10, step: 2} 
|> Map.new(fn {k, v} -> 
    {Atom.to_string(k), v} 
end)
|> IO.inspect
%{first: 1, last: 10, step: 2} 
|> Map.new(&{Atom.to_string(elem(&1, 0)), elem(&1, 1)})
|> IO.inspect
#方法その2
for {k, v} <- %{first: 1, last: 10, step: 2}, into: %{}, do: {Atom.to_string(k), v}
|> IO.inspect
#方法その3
#%{first: 1, last: 10, step: 2} 
#|> Jason.encode! 
#|> Jason.decode!
#|> IO.inspect
#String-key map-list → Atom-key map-list
[
  %{"first" => 1, "last" => 10, "step" => 2}, 
  %{"first" => 9, "last" => 4, "step" => -1}, 
] 
|> Enum.map(& Map.new(&1, fn {k, v} -> 
	{String.to_atom(k), v} 
end))
|> IO.inspect
#Atom-key map-list → String-key map-list
[
  %{first: 1, last: 10, step: 2}, 
  %{first: 9, last: 4, step: -1}, 
] 
|> Enum.map(& Map.new(&1, fn {k, v} -> 
	{Atom.to_string(k), v} 
end))
|> IO.inspect
#Transpose list-list
[
  [nil, 2.0, 3.0],
  [0.9, nil, 3.3]
] 
|> List.zip 
|> Enum.map(fn n -> 
	Tuple.to_list(n) 
end)
|> IO.inspect
#map-value-list to list-map (String-key & Atom-key)
%{
  x: [nil, 2.0, 3.0],
  y: [0.9, nil, 3.3]
} 
|> then(& Enum.map(Map.values(&1) 
|> List.zip 
|> Enum.map(fn n -> Tuple.to_list(n) end), fn row -> 
	Enum.into(List.zip([Map.keys(&1), row]), %{}) 
end))
|> IO.inspect
#Keyword-list list → Atom-key map-list
[
  [first: 1, last: 10, step: 2], 
  [first: 9, last: 4, step: -1], 
] 
|> Enum.map(& Map.new(&1, fn {k, v} -> 
	{k, v} 
end))
|> IO.inspect
#Keyword-list list → String-key map-list
[
  [first: 1, last: 10, step: 2], 
  [first: 9, last: 4, step: -1], 
] 
|> Enum.map(& Map.new(&1, fn {k, v} -> 
	{Atom.to_string(k), v} 
end))
|> IO.inspect
#Tuple-list → Atom-key map
[
  {:first, 1}, 
  {:last, 10}, 
  {:step, 2}, 
] 
|> Enum.into(%{})
|> IO.inspect
#Tuple-list → String-key map-list
[
  {:first, 1}, 
  {:last, 10}, 
  {:step, 2}, 
] 
|> Enum.map(fn {k, v} -> 
	{Atom.to_string(k), v} 
end) 
|> Enum.into(%{})
|> IO.inspect
#Tuple-set-list → Atom-key map-list
[
  {:first, 1, :last, 10, :step, 2}, 
  {:first, 9, :last, 4, :step, -1}, 
] 
|> Enum.map(& &1 |> Tuple.to_list |> Enum.chunk_every(2) |> Enum.map(fn n -> 
	List.to_tuple(n) 
end) |> Enum.into(%{}))
|> IO.inspect
#Tuple-set-list → String-key map-list
[
  {:first, 1, :last, 10, :step, 2}, 
  {:first, 9, :last, 4, :step, -1}, 
] 
|> Enum.map(& &1 |> Tuple.to_list |> Enum.chunk_every(2) |> Enum.map(fn n -> 
	List.to_tuple(n) 
end) |> Enum.into(%{}, fn {k, v} -> 
	{Atom.to_string(k), v} 
end))
|> IO.inspect
#Fill-missings from float/nil in map-value-list
%{
  x: [nil, 2.0, 3.0],
  y: [0.9, nil, 3.3]
} 
|> Enum.map(fn {k, v} -> 
	{k, v |> Enum.map(& if is_nil(&1), do: 0.0, else: &1)} 
end) 
|> Enum.into(%{})
|> IO.inspect
#Fill-missings from string in map-value-list
#方法その1
%{
  x: ["", "hoge", "foo"],
  y: ["abc", "", "xyz"]
} 
|> Enum.map(fn {k, v} -> 
	{k, v |> Enum.map(& String.replace(&1, ~r/^$/, "ddd"))} 
end) 
|> Enum.into(%{})
|> IO.inspect
#方法その2
%{
  x: ["", "hoge", "foo"],
  y: ["abc", "", "xyz"]
} 
|> Enum.map(fn {k, v} -> 
	{k, v |> Enum.map(& if &1 == "", do: "ddd", else: &1)} 
end) 
|> Enum.into(%{})
|> IO.inspect
#Fill-missings from string/nil in map-value-list
%{
  x: [nil, "hoge", "foo"],
  y: ["abc", "", "xyz"]
} 
|> Enum.map(fn {k, v} -> 
	{k, v |> Enum.map(& if is_nil(&1) || &1 == "", do: "ddd", else: &1)} 
end) 
|> Enum.into(%{})
|> IO.inspect
#Fill-missings from float in list-map
[
  %{x: nil, y: 0.9},
  %{x: 2.0, y: nil},
  %{x: 3.0, y: 3.3}
] 
|> Enum.map(& &1 |> Enum.map(fn {k, v} -> 
	{k, (if is_nil(v), do: 0.0, else: v)} 
end) |> Enum.into(%{}))
|> IO.inspect
#Fill-missings from string in list-map
[
  %{x: "", y: "abc"},
  %{x: "hoge", y: ""},
  %{x: "foo", y: "xyz"}
] 
|> Enum.map(& &1 |> Enum.map(fn {k, v} -> 
	{k, String.replace(v, ~r/^$/, "ddd")} 
end) |> Enum.into(%{}))
|> IO.inspect
#Fill-missings from string/nil in list-map
[
  %{x: nil, y: "abc"},
  %{x: "hoge", y: ""},
  %{x: "foo", y: "xyz"}
] 
|> Enum.map(& &1 |> Enum.map(fn {k, v} -> 
	{k, (if is_nil(v) || v == "", do: "ddd", else: v)} 
end) |> Enum.into(%{}))
|> IO.inspect
#Float-string to float
"12.34" 
|> String.replace(~r/^(?!.*\.).*$/, "\\0\.0") 
|> String.to_float
|> IO.inspect
#Integer-string to float
"123" 
|> String.replace(~r/^(?!.*\.).*$/, "\\0\.0") 
|> String.to_float
|> IO.inspect
#Integer/Float-string to float or keep String
v = "'abc'"
v = "\"abc\""
v = "abc"
v = "12.34"
v = "123"
v = "12ab34"
case String.match?(v, ~r/^([0-9]|\.)+$/) do
  	true -> 
    	case String.match?(v, ~r/\./) do
      	true -> 
			String.to_float(v)
      	_ -> 
			String.to_integer(v)
    	end
  	_ -> 
		v 
		|> String.trim("'") 
		|> String.trim("\"")
end
|> IO.inspect





```

# 実行結果

```
1..10//2
nil..nil//nil
%{first: 1, last: 10, step: 2}
%{first: 2, last: 10, step: 2}
%{first: 3, last: 10, step: 2}
%{first: 1, last: 10, step: 2}
%{first: 1, last: 10, step: 2}
{:first, 1}
{:last, 10}
{:step, 2}
%{"first" => 1, "last" => 10, "step" => 2}
%{"first" => 1, "last" => 10, "step" => 2}
{"first", 1}
{"last", 10}
{"step", 2}
[%{first: 1, last: 10, step: 2}, %{first: 9, last: 4, step: -1}]
[
  %{"first" => 1, "last" => 10, "step" => 2},
  %{"first" => 9, "last" => 4, "step" => -1}
]
[[nil, 0.9], [2.0, nil], [3.0, 3.3]]
[%{x: nil, y: 0.9}, %{x: 2.0, y: nil}, %{x: 3.0, y: 3.3}]
[%{first: 1, last: 10, step: 2}, %{first: 9, last: 4, step: -1}]
[
  %{"first" => 1, "last" => 10, "step" => 2},
  %{"first" => 9, "last" => 4, "step" => -1}
]
%{first: 1, last: 10, step: 2}
%{"first" => 1, "last" => 10, "step" => 2}
[%{first: 1, last: 10, step: 2}, %{first: 9, last: 4, step: -1}]
[
  %{"first" => 1, "last" => 10, "step" => 2},
  %{"first" => 9, "last" => 4, "step" => -1}
]
%{x: [0.0, 2.0, 3.0], y: [0.9, 0.0, 3.3]}
%{x: ["ddd", "hoge", "foo"], y: ["abc", "ddd", "xyz"]}
%{x: ["ddd", "hoge", "foo"], y: ["abc", "ddd", "xyz"]}
%{x: ["ddd", "hoge", "foo"], y: ["abc", "ddd", "xyz"]}
[%{x: 0.0, y: 0.9}, %{x: 2.0, y: 0.0}, %{x: 3.0, y: 3.3}]
[%{x: "ddd", y: "abc"}, %{x: "hoge", y: "ddd"}, %{x: "foo", y: "xyz"}]
[%{x: "ddd", y: "abc"}, %{x: "hoge", y: "ddd"}, %{x: "foo", y: "xyz"}]
12.34
123.0
"12ab34"
```

# 成果物

https://paiza.io/projects/ZqtIEb5A1O3RSuoWKDmOVQ


以上。