スポンサーリンク

2015年8月3日

[Elixir]基本的な型を習得する

とある錬金術師の万能薬(Elixir)

Goal

Elixirの基本的な型を習得する。

Dev-Environment

OS: Windows8.1
Erlang: Eshell V6.4, OTP-Version 17.5
Elixir: v1.0.4

Wait a minute

一番基本的なところですね。
型分からないと色々どうしようもないですし。

Index

|> Types List
|> Basic arithmetic
|> 2,8,16 (Binary number, Octal notation, Hexadecimal)
|> Binary
|> list or taple
|> Extra

Types List

基本的な型の一覧。
(これ以外にもある)
  • Integer (整数)
Example:
iex(1)> 1
1
  • Float number (小数)
Example:
iex(2)> 1.0
1.0
  • Boolean (真偽値)
Example:
iex(3)> true
true
iex(4)> false
false
  • Atom (アトム)
Example:
iex(5)> :atom
:atome
iex(6)> :elixir
:elixir
  • String (文字列)
Example:
iex(5)> "Hello World!!"
"Hello World!!"
iex(6)> "Elixir"
"Elixir"
Description:
‘(単一引用符)で囲っても文字列になるが、意味合いが変わる。
文字のリストとして扱われる。
基本、”(二重引用符)を使っていれば問題ない。
  • Anonymous functions (匿名関数)
基本の型であることに驚き・・・応用だと思うが。
匿名関数の定義方法。
iex(7)> add = fn a, b -> a + b end
#Function<12.90072148/2 in :erl_eval.expr/5>
iex(8)> is_function(add)
true
iex(9)> is_function(add, 2)
true
iex(10)> is_function(add, 1)
false
iex(11)> is_function(add, 3)
false
iex(12)> add.(1, 2)
3
Description:
整数や文字列と同じように関数の引数にも使える。
Caution:
定義してないとエラーになる。(そりゃそうだ)
iex(13)> is_function(sub)
** (RuntimeError) undefined function: sub/0

iex(13)> sub.(2, 1)
** (RuntimeError) undefined function: sub/0
また利用する時は・・・変数.括弧といった形でドットが必要。
iex(13)> add(1, 2)
** (RuntimeError) undefined function: add/2
  • List (リスト)
リストの定義方法。
iex(14)> [1, 2, 3]
[1, 2, 3]
iex(15)> [1, 1.0, true, "string", :atom]
[1, 1.0, true, "string", :atom]
リストの足し引き
iex(16)> [1, 2, 3] ++ [4, 5, 6]
[1, 2, 3, 4, 5, 6]
iex(17)> [1, true, 2, false, 3, true] -- [true, false]
[1, 2, 3, true]
headとtail
iex(18)> hd([1, 2, 3])
1
iex(19)> tl([1, 2, 3])
[2, 3]
Caution:
空リストだとエラーになる。
  • Tuple (タプル)
配列みたいなもの。
タプルの定義方法。{}(中括弧)を使う。
iex(20)> {1, 2, 3}
{1, 2, 3}
iex(21)> {1, 1.0, true, "string", :atom}
{1, 1.0, true, "string", :atom}
サイズ取得。
iex(22)> tuple_size({1, 1.0, true, "string", :atom})
5
要素へのアクセス方法。
iex(23)> elem({1, 1.0, true, "string", :atom}, 0)
1
iex(24)> elem({1, 1.0, true, "string", :atom}, 4)
:atom
iex(25)> elem({1, 1.0, true, "string", :atom}, 5)
** (ArgumentError) argument error
    :erlang.element(6, {1, 1.0, true, "string", :atom})
Description:
インデックスは0から開始。
要素の変更。
iex(25)> put_elem({1, 1.0, true, "string", :atom}, 2, false)
{1, 1.0, false, "string", :atom}
Caution:
新しいタプルが返ってくる。
元のタプルは変わらない。
これはエラーになる。
iex(26)> put_elem({1, 1.0, true, "string", :atom}, 5, false)
** (ArgumentError) argument error
    :erlang.setelement(6, {1, 1.0, true, "string", :atom}, false)

Basic arithmetic

簡単に四則演算をしてみる。
Example:
iex(26)> 2 + 2
4
iex(27)> 2 - 2
0
iex(28)> 2 * 2
4
iex(29)> 2 / 2
1.0
iex(30)> div(2, 2)
1
iex(31)> rem(2, 2)
0

2,8,16 (Binary number, Octal notation, Hexadecimal)

2進数、8進数、16進数の定義方法。
iex(32)> 0b0101
5
iex(33)> 0o111
73
iex(34)> 0x1F
31

Binary

バイナリの定義方法。(後で詳しく出てくる)
Getting Startにはない項目。
2進数と混在させないため、一応書いておく。
iex(35)> <<0, 1, 2, 3>>
<<0, 1, 2, 3>>
これがまた色々とできる。

list or taple

リストとタプルのどちらを使い分けはどうすればいいんでしょうか?
  • リスト
    • 直線的な操作
    • サイズ取得が遅い
    • 手前の要素追加は速い
    • 最後尾の要素追加は遅い
  • タプル
    • サイズ取得が速い
    • 要素へのアクセス速い
    • 要素の追加、更新は遅い

Extra

タプルの一般的な使い方。
関数の戻り値に情報を付与することがある。
こんな感じに・・・
{:ok, "ok"}
{:error, "error"}
Phoenixのソースコードで申し訳ないんですけど・・・
実際に、こんな感じで使うことができる。
Example:
def create(conn, %{"login_params" => %{"email" => email, "password" => password}}) do
  case login(email, password) do
    {:ok, user} ->
      conn
      |> put_flash(:info, "User login is success!!")
      |> put_session(:user_id, user.id)
      |> redirect(to: static_pages_path(conn, :home))
    :error ->
      conn
      |> put_flash(:error, "User login is failed!! email or password is incorrect.")
      |> redirect(to: session_path(conn, :new))
  end
end

defp login(email, password) do
  user = SampleApp.User.find_user_from_email(email)
  case authentication(user, password) do
    true -> {:ok, user}
       _ -> :error
  end
end
Elixirの命名規則について少し・・・
操作が一定時間になるものは関数名sizeと命名。
明示的に計算が必要になるものは関数名lengthと命名。
こういった規則があるそうです。

Speaking to oneself

基本的な型なのに記事作りながらやると、やっぱ時間かかるな・・・
これで基本の型は問題なしですね。
ロゴを作ってみたwww

Bibliography

人気の投稿