LoginSignup
3
0

Elixirの対話モードで制御構造の動作を確認してみた

Posted at

概要

Elixirの対話モードで制御構造の動作を確認してみました。以下のページを参考にしました。

対話モードで実行

以下のコマンドを実行しました。

$ iex
Erlang/OTP 24 [erts-12.2.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit]

Interactive Elixir (1.12.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> if String.valid?("Hello") do
...(1)> "Valid string!"
...(1)> else
...(1)> "Invalid string."
...(1)> end
"Valid string!"
iex(2)> if "a string value" do
...(2)> "Truthy"
...(2)> end
"Truthy"
iex(3)> unless is_integer("hello") do
...(3)> "Not an Int"
...(3)> end
"Not an Int"
iex(4)> case {:ok, "Hello World"} do
...(4)> {:ok, result} -> result
...(4)> {:error} -> "Uh oh!"
...(4)> _ -> "Catch all"
...(4)> end
"Hello World"
iex(5)> case :even do
...(5)> :odd -> "Odd"
...(5)> end    
** (CaseClauseError) no case clause matching: :even

iex(5)> case :even do
...(5)> :odd -> "Odd"
...(5)> _ -> "Not Odd"
...(5)> end
"Not Odd"
iex(6)> pie = 3.14
3.14
iex(7)> case "cherry pie" do
...(7)> ^pie -> "Not so tasty"
...(7)> pie -> "I bet #{pie} is tasty"
...(7)> end
"I bet cherry pie is tasty"
iex(8)> case {1, 2, 3} do
...(8)> {1, x, 3} when x > 0 ->
...(8)> "Will match"
...(8)> _ ->
...(8)> "Won't match"
...(8)> end
"Will match"
iex(9)> cond do
...(9)> 2 + 2 == 5 ->
...(9)> "This will not be true"
...(9)> 2 * 2 == 3 ->
...(9)> "Nor this"
...(9)> 1 + 1 == 2 ->
...(9)> "But this will"
...(9)> end
"But this will"
iex(10)> cond do
...(10)> 7 + 1 == 0 -> "Incorrect"
...(10)> true -> "Catch all"
...(10)> end
"Catch all"
iex(11)> user = %{first: "Sean", last: "Callan"}
%{first: "Sean", last: "Callan"}
iex(12)> with {:ok, first} <- Map.fetch(user, :first),
...(12)> {:ok, last} <- Map.fetch(user, :last),
...(12)> do: last <> ", " <> first
"Callan, Sean"
iex(13)> user = %{first: "doomspork"}
%{first: "doomspork"}
iex(14)> with {:ok, first} <- Map.fetch(user, :first),
...(14)> {:ok, last} <- Map.fetch(user, :last),
...(14)> do: last <> ", " <> first
:error
iex(15)> case Repo.insert(changeset) do
...(15)> {:ok, user} ->
...(15)> case Guardian.encode_and_sign(user, :token, claims) do
...(15)> {:ok, jwt, full_claims} ->
...(15)> important_stuff(jwt, full_claims)
...(15)> error ->
...(15)> error
...(15)> end
...(15)> error ->
...(15)> error
...(15)> end
** (CompileError) iex:15: undefined function changeset/0
    (stdlib 3.17) lists.erl:1358: :lists.mapfoldl/3
iex(15)> with {:ok, user} <- Repo.insert(changeset),
...(15)> {:ok, jwt, full_claims} <- Guardian.encode_and_sign(user, :token, claims),
...(15)> do: important_stuff(jwt, full_claims)
** (CompileError) iex:15: undefined function changeset/0
    (stdlib 3.17) lists.erl:1358: :lists.mapfoldl/3
    (stdlib 3.17) lists.erl:1358: :lists.mapfoldl/3
iex(15)> import Integer
Integer
iex(16)> m = %{a: 1, c: 3}
%{a: 1, c: 3}
iex(17)> a =
...(17)> with {:ok, number} <- Map.fetch(m, :a),
...(17)> true <- is_even(number) do
...(17)> IO.puts "#{number} divided by 2 is #{div(number, 2)}"
...(17)> :even
...(17)> else
...(17)> :error ->
...(17)> IO.puts("We don't have this item in map")
...(17)> :error
...(17)> _ -> 
...(17)> IO.puts("It is odd")
...(17)> :odd
...(17)> end
It is odd
:odd
iex(18)> 

まとめ

何かの役に立てばと。

3
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
3
0