Home

Elixir Conditionals

If

iex> if String.valid?("Hello") do ...> "Valid string!" ...> else ...> "Invalid string." ...> end "Valid string!" iex> if "a string value" do ...> "Truthy" ...> end "Truthy"

Unless

iex> unless is_integer("hello") do ...> "Not an Int" ...> end "Not an Int"

Case

iex> case {:ok, "Hello World"} do ...> {:ok, result} -> result ...> {:error} -> "Uh oh!" ...> _ -> "Catch all" ...> end "Hello World"

_ is an important inclusion too:

iex> case :even do ...> :odd -> "Odd" ...> end ** (CaseClauseError) no case clause matching: :even iex> case :even do ...> :odd -> "Odd" ...> _ -> "Not Odd" ...> end "Not Odd"

With pinning:

iex> pie = 3.14 3.14 iex> case "cherry pie" do ...> ^pie -> "Not so tasty" ...> pie -> "I bet #{pie} is tasty" ...> end "I bet cherry pie is tasty"

Repository

https://github.com/okeeffed/developer-notes-nextjs/content/elixir/elixir-conditionals

Sections


Related