paiza.ioでelixir その234

# 概要
paiza.ioでelixirやってみた。
良いコードを見つけたので、やってみた。

# 参考にしたページ。

https://qiita.com/nori-channel/items/96d7c1c94de14b5dc743

# サンプルコード

```
sum = fn (a, b) -> 
    a + b 
end
|> IO.inspect
sum.(2, 3)
|> IO.inspect
sum = &(&1 + &2)
|> IO.inspect
sum.(2, 3)
|> IO.inspect
handle_result = fn
    {:ok, result} -> IO.puts "Handling result..."
    {:ok, _} -> IO.puts "This would be never run as previous will be matched beforehand."
    {:error} -> IO.puts "An error has occurred!"
end
|> IO.inspect
some_result = 1
|> IO.inspect
handle_result.({:ok, some_result})
|> IO.inspect
handle_result.({:error})
|> IO.inspect
defmodule Greeter do
    def hello(name) do
        "Hello, " <> name
    end
end
|> IO.inspect
Greeter.hello("Sean")
|> IO.inspect
defmodule Greeter do
    def hello(name), do: "Hello, " <> name
end
|> IO.inspect
defmodule Length do
    def of([]), do: 0
    def of([_ | tail]), do: 1 + of(tail)
end
|> IO.inspect
Length.of []
|> IO.inspect
Length.of [1, 2, 3]
|> IO.inspect
defmodule Greeter2 do
    def hello(), do: "Hello, anonymous person!"
    def hello(name), do: "Hello, " <> name
    def hello(name1, name2), do: "Hello, #{name1} and #{name2}"
end
|> IO.inspect
Greeter2.hello()
|> IO.inspect
Greeter2.hello("Fred")
|> IO.inspect
Greeter2.hello("Fred", "Jane")
|> IO.inspect
#defmodule Greeter1 do
#    def hello(%{name: person_name}) do
#        IO.puts "Hello, " <> person_name
#    end
#end
|> IO.inspect
fred = %{name: "Fred", age: "95", favorite_color: "Taupe"}
|> IO.inspect
#Greeter1.hello(fred)
|> IO.inspect
#Greeter1.hello(%{age: "95", favorite_color: "Taupe"})
|> IO.inspect
#defmodule Greeter2 do
#    def hello(%{name: person_name} = person) do
#        IO.puts "Hello, " <> person_name
#        IO.inspect person
#    end
#end
|> IO.inspect
#Greeter2.hello(fred)
|> IO.inspect
#Greeter2.hello(%{name: "Fred"})
|> IO.inspect
#Greeter2.hello(%{age: "95", favorite_color: "Taupe"})
|> IO.inspect
defmodule Greeter3 do
    def hello(person = %{name: person_name}) do
        IO.puts "Hello, " <> person_name
        IO.inspect person
    end
end
|> IO.inspect
Greeter3.hello(fred)
|> IO.inspect
defmodule Greeter do
    def hello(name), do: phrase() <> name
    defp phrase, do: "Hello, "
end
|> IO.inspect
Greeter.hello("Sean")
|> IO.inspect
#Greeter.phrase
|> IO.inspect
defmodule Greeter do
    def hello(names) when is_list(names) do
        names = Enum.join(names, ", ")
        hello(names)
    end
    def hello(name) when is_binary(name) do
        phrase() <> name
    end
    defp phrase, do: "Hello, "
end
|> IO.inspect
Greeter.hello ["Sean", "Steve"]
|> IO.inspect
defmodule Greeter do
    def hello(name, language_code \\ "en") do
        phrase(language_code) <> name
    end
    defp phrase("en"), do: "Hello, "
    defp phrase("es"), do: "Hola, "
end
|> IO.inspect
Greeter.hello("Sean", "en")
|> IO.inspect
Greeter.hello("Sean")      
|> IO.inspect
Greeter.hello("Sean", "es")
|> IO.inspect
defmodule Greeter do
    def hello(names, language_code \\ "en") when is_list(names) do
        names = Enum.join(names, ", ")
        hello(names, language_code)
    end
    #def hello(name, language_code \\ "en") when is_binary(name) do
    #    phrase(language_code) <> name
    #end
    defp phrase("en"), do: "Hello, "
    defp phrase("es"), do: "Hola, "
end
|> IO.inspect
defmodule Greeter do
    def hello(names, language_code \\ "en")
    def hello(names, language_code) when is_list(names) do
        names = Enum.join(names, ", ")
        hello(names, language_code)
    end
    def hello(name, language_code) when is_binary(name) do
        phrase(language_code) <> name
    end
    defp phrase("en"), do: "Hello, "
    defp phrase("es"), do: "Hola, "
end
|> IO.inspect
Greeter.hello ["Sean", "Steve"]
|> IO.inspect
Greeter.hello ["Sean", "Steve"], "es"
|> IO.inspect




```

# 実行結果

```
#Function<0.35002210 in file:Main.exs>
5
5
5
#Function<2.35002210 in file:Main.exs>
1
Handling result...
:ok
An error has occurred!
:ok
{:module, Greeter,
 <<70, 79, 82, 49, 0, 0, 5, 120, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 153,
   0, 0, 0, 16, 14, 69, 108, 105, 120, 105, 114, 46, 71, 114, 101, 101, 116,
   101, 114, 8, 95, 95, 105, 110, 102, 111, 95, ...>>, {:hello, 1}}
"Hello, Sean"
{:module, Greeter,
 <<70, 79, 82, 49, 0, 0, 5, 116, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 153,
   0, 0, 0, 16, 14, 69, 108, 105, 120, 105, 114, 46, 71, 114, 101, 101, 116,
   101, 114, 8, 95, 95, 105, 110, 102, 111, 95, ...>>, {:hello, 1}}
{:module, Length,
 <<70, 79, 82, 49, 0, 0, 5, 44, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 137,
   0, 0, 0, 15, 13, 69, 108, 105, 120, 105, 114, 46, 76, 101, 110, 103, 116,
   104, 8, 95, 95, 105, 110, 102, 111, 95, 95, ...>>, {:of, 1}}
[]
[1, 2, 3]
{:module, Greeter2,
 <<70, 79, 82, 49, 0, 0, 7, 136, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 184,
   0, 0, 0, 18, 15, 69, 108, 105, 120, 105, 114, 46, 71, 114, 101, 101, 116,
   101, 114, 50, 8, 95, 95, 105, 110, 102, 111, ...>>, {:hello, 2}}
"Hello, anonymous person!"
"Hello, Fred"
"Hello, Fred and Jane"
"Hello, Fred and Jane"
%{age: "95", favorite_color: "Taupe", name: "Fred"}
%{age: "95", favorite_color: "Taupe", name: "Fred"}
%{age: "95", favorite_color: "Taupe", name: "Fred"}
%{age: "95", favorite_color: "Taupe", name: "Fred"}
%{age: "95", favorite_color: "Taupe", name: "Fred"}
%{age: "95", favorite_color: "Taupe", name: "Fred"}
%{age: "95", favorite_color: "Taupe", name: "Fred"}
{:module, Greeter3,
 <<70, 79, 82, 49, 0, 0, 6, 44, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 182,
   0, 0, 0, 20, 15, 69, 108, 105, 120, 105, 114, 46, 71, 114, 101, 101, 116,
   101, 114, 51, 8, 95, 95, 105, 110, 102, 111, ...>>, {:hello, 1}}
Hello, Fred
%{age: "95", favorite_color: "Taupe", name: "Fred"}
%{age: "95", favorite_color: "Taupe", name: "Fred"}
{:module, Greeter,
 <<70, 79, 82, 49, 0, 0, 5, 152, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 159,
   0, 0, 0, 17, 14, 69, 108, 105, 120, 105, 114, 46, 71, 114, 101, 101, 116,
   101, 114, 8, 95, 95, 105, 110, 102, 111, 95, ...>>, {:phrase, 0}}
"Hello, Sean"
"Hello, Sean"
{:module, Greeter,
 <<70, 79, 82, 49, 0, 0, 6, 84, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 176,
   0, 0, 0, 19, 14, 69, 108, 105, 120, 105, 114, 46, 71, 114, 101, 101, 116,
   101, 114, 8, 95, 95, 105, 110, 102, 111, 95, ...>>, {:phrase, 0}}
["Sean", "Steve"]
{:module, Greeter,
 <<70, 79, 82, 49, 0, 0, 6, 220, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 159,
   0, 0, 0, 17, 14, 69, 108, 105, 120, 105, 114, 46, 71, 114, 101, 101, 116,
   101, 114, 8, 95, 95, 105, 110, 102, 111, 95, ...>>, {:phrase, 1}}
"Hello, Sean"
"Hello, Sean"
"Hola, Sean"
{:module, Greeter,
 <<70, 79, 82, 49, 0, 0, 6, 132, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 156,
   0, 0, 0, 16, 14, 69, 108, 105, 120, 105, 114, 46, 71, 114, 101, 101, 116,
   101, 114, 8, 95, 95, 105, 110, 102, 111, 95, ...>>, {:phrase, 1}}
{:module, Greeter,
 <<70, 79, 82, 49, 0, 0, 7, 180, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 176,
   0, 0, 0, 19, 14, 69, 108, 105, 120, 105, 114, 46, 71, 114, 101, 101, 116,
   101, 114, 8, 95, 95, 105, 110, 102, 111, 95, ...>>, {:phrase, 1}}
["Sean", "Steve"]
"es"
```

# 成果物

https://paiza.io/projects/HQF7tg8k2Pjihl_qqKy5Ow


以上。