概要
paiza.ioでelixirやってみた。
良いコードを見つけたので、やってみた。
参考にしたページ。
サンプルコード
if String.valid?("Hello") do
"Valid string!"
else
"Invalid string."
end
|> IO.inspect
if "a string value" do
"Truthy"
end
|> IO.inspect
unless is_integer("hello") do
"Not an Int"
end
|> IO.inspect
case {:ok, "Hello World"} do
{:ok, result} ->
result
{:error} ->
"Uh oh!"
_ ->
"Catch all"
end
|> IO.inspect
#case :even do
# :odd ->
# "Odd"
#end
|> IO.inspect
case :even do
:odd ->
"Odd"
_ ->
"Not Odd"
end
|> IO.inspect
pie = 3.14
|> IO.inspect
case "cherry pie" do
^pie ->
"Not so tasty"
pie ->
"I bet #{pie} is tasty"
end
|> IO.inspect
case {1, 2, 3} do
{1, x, 3} when x > 0 ->
"Will match"
_ ->
"Won't match"
end
|> IO.inspect
cond do
2 + 2 == 5 ->
"This will not be true"
2 * 2 == 3 ->
"Nor this"
1 + 1 == 2 ->
"But this will"
end
|> IO.inspect
cond do
7 + 1 == 0 ->
"Incorrect"
true ->
"Catch all"
end
|> IO.inspect
user = %{first: "Sean", last: "Callan"}
|> IO.inspect
with {:ok, first} <- Map.fetch(user, :first), {:ok, last} <- Map.fetch(user, :last), do: last <> ", " <> first
|> IO.inspect
user = %{first: "doomspork"}
|> IO.inspect
with {:ok, first} <- Map.fetch(user, :first), {:ok, last} <- Map.fetch(user, :last), do: last <> ", " <> first
|> IO.inspect
#case Repo.insert(changeset) do
# {:ok, user} ->
# case Guardian.encode_and_sign(user, :token, claims) do
# {:ok, jwt, full_claims} ->
# important_stuff(jwt, full_claims)
# error ->
# error
# end
# error ->
# error
#end
|> IO.inspect
#with {:ok, user} <- Repo.insert(changeset), {:ok, jwt, full_claims} <- Guardian.encode_and_sign(user, :token, claims), do: important_stuff(jwt, full_claims)
|> IO.inspect
#import Integer
|> IO.inspect
#m = %{a: 1, c: 3}
|> IO.inspect
#a = with {:ok, number} <- Map.fetch(m, :a), true <- is_even(number) do
# IO.puts "#{number} divided by 2 is #{div(number, 2)}"
# :even
#else
# :error ->
# IO.puts("We don't have this item in map")
# :error
# _ ->
# IO.puts("It is odd")
# :odd
#end
|> IO.inspect
実行結果
"Valid string!"
"Truthy"
"Not an Int"
"Hello World"
"Hello World"
"Not Odd"
3.14
"I bet cherry pie is tasty"
"Will match"
"But this will"
"Catch all"
%{first: "Sean", last: "Callan"}
"Callan, Sean"
%{first: "doomspork"}
成果物
以上。