2
2

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 1 year has passed since last update.

口で言うより行うことがErlang習得への近道と信じています。

case式の書き方

case 式 of
  パターン1 [when ガードシーケンス1] ->
    本文1;
  パターンN [when ガードシーケンスN] ->
    本文N
end
  1. caseofの間にある式が評価される
  2. その結果に対してパターンが順次マッチングされる
  3. マッチングが成功し、ガードシーケンスが真であれば、対応する本文が評価される
  4. 本文の戻り値がcase式の戻り値となる
  5. 真のガードシーケンスを持つパターンがない場合、case_clauseランタイムエラーが発生する

試しに練習目的でcase式を使った簡単な無名関数を書いてみます。受け取った引数のパターンに応じて真偽を判定するだけの関数です。無名関数なので、Erlangシェルに貼り付けて使えます。

IsValidResult = fun (Result) ->
  case Result of
    {ok, _Value} ->
      true;
    error ->
      true;
    _Else ->
      false
  end
end.
> IsValidResult({ok, 123}).
true

> IsValidResult(error).
true

> IsValidResult([1, 2, 3]).
false

Elixirにも挑戦したい

闘魂ElixirシリーズとElixir Schoolがオススメです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?