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

ElixirAdvent Calendar 2023

Day 14

美しい!!Elixirの関数定義

Last updated at Posted at 2023-12-13

Elixirの利用者の一人の意見です

Elixirの関数の定義は、関数型言語らしい特徴があります。

数学での関数の定義は、xの範囲に応じて定義する表記ができます。

f(x) = \left\{ \begin{array}{l}
\displaystyle x ( x >= 0 ) \\
0  ( x < 0 )
\end{array} \right.

Pythonでこの関数を書いてみると

def f(x) do
    if x >= 0:
        return x
    else:
        return 0

一つの関数の中で場合分けする記述でしか書けません。

Elixirの場合

def f(x) when x >= 0, do: x
def f(x) when x < 0, do: 0

美しい!
実に美しい!

12
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
12
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?