2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Elixir case / cond / if

Last updated at Posted at 2014-09-26

Elixir case / cond / if

概要

Elixir の case / cond / if について

case

複数のパターンとのパターンマッチを行う。
ガード句として when を利用可能。

iex> use_case = fn a ->
...>   case a do
...>     [1,x,3] when x > 0 ->
...>       "hit1-3, x > 0"
...>     [1,x,3] ->
...>       "hit1-3, x <= 0"
...>     [4,5,6] ->
...>       "hit4-6"
...>     _ ->
...>       "not hit"
...>   end
...> end
# Function<erl_eval.6.82930912>
iex> use_case.([1,2,3])
"hit1-3, x > 0"
iex> x
2
iex> use_case.([1,0,3])
"hit1-3, x <= 0"
iex> use_case.([4,5,6])
"hit4-6"
iex> use_case.([4,5,7])
"not hit"

Expressions in guard clauses.

ガード句で利用可能な関数について。
詳しいリストについては下記参照
http://elixir-lang.org/getting_started/5.html#5.2-expressions-in-guard-clauses.

ガード句での利用例

iex> guard_sample = fn a ->
...>   case a do
...>     x when is_float(x) ->
...>       "float"
...>     x when is_list(x) and is_atom(hd(x)) ->
...>       "atom list"
...>     x when is_list(x) ->
...>       "list"
...>     _ ->
...>       "not hit"
...>   end
...> end
# Function<erl_eval.6.82930912>
iex> guard_sample.(1.1)
"float"
iex> guard_sample.([1.1,:hoge])
"list"
iex> guard_sample.([:hige,:hoge])
"atom list"
iex> guard_sample.(:hoge)
"not hit"

cond

異なる条件で分岐を判定したい場合に cond を利用する。
他言語でいうところの elseif にあたる

iex> cond_sample = fn a,b ->
...>   cond do
...>     a+a==b ->
...>       "plus"
...>     a*a==b ->
...>       "multiply"
...>     true ->
...>       "not match"
...>   end
...> end
# Function<erl_eval.12.82930912>
iex> cond_sample.(1,2)
"plus"
iex> cond_sample.(1,1)
"multiply"
iex> cond_sample.(1,3)
"not match"

if / unless

if

iex> use_if = fn bool ->
...>   if bool do
...>     "true!"
...>   end
...> end
# Function<erl_eval.6.82930912>
iex> use_if.(true)
"true!"
iex> use_if.(false)
nil

unless

iex> use_unless = fn bool ->
...>   unless bool do
...>     "false!"
...>   end
...> end
# Function<erl_eval.6.82930912>
iex> use_unless.(false)
"false!"
iex> use_unless.(true)
nil

block

if do-end

iex> use_if = fn bool ->
...>   if bool do
...>     "true!"
...>   end
...> end
# Function<erl_eval.6.82930912>
iex> use_if.(true)
"true!"
iex> use_if.(false)
nil

if do:

1行にまとめて記述

iex> use_if_do_coron = fn bool ->
...>   if bool, do: "true!"
...> end
# Function<erl_eval.6.82930912>
iex> use_if_do_coron.(true)
"true!"
iex> use_if_do_coron.(false)
nil

if do: else:

iex> use_ternary_operator = fn a ->
...>   if a, do: "true", else: "false"
...> end
# Function<erl_eval.6.82930912>
iex> use_ternary_operator.(true)
"true"
iex> use_ternary_operator.(false)
"false"

参照

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?