LoginSignup
4
4

More than 5 years have passed since last update.

Elixir初心者がElixirを動かしてみた際のメモ

Posted at

Elixirの特徴

環境整備

わしのマシン

  • OS X Yosemite
  • Ver 10.10.5

インストールコマンド

brew install elixir

インストール確認

$ iex -v
Erlang/OTP 19 [erts-8.1] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

IEx 1.3.4

Elixirの実行

  • コードの動作確認は対話シェル上で行う
$ iex
Erlang/OTP 19 [erts-8.1] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Interactive Elixir (1.3.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> IO.puts("Get Wild")
Get Wild
:ok
iex(2)>

基本的なデータの型

https://elixirschool.com/jp/lessons/basics/basics/#section-2
ここを見ておけばOK。

個人的なメモ

整数の除算

浮動小数点で返ってくる

iex(365)> 10 / 2
5.0
iex(366)>

整数で戻り値を受け取りたい場合はdiv関数を使う

iex(366)> div(10,2)
5
iex(367)>

余りはrem関数を使って計算する

iex(373)> rem(10, 3)
1
iex(374)>

文字列の扱い

ダブルクォーテーションで囲った場合

iex(14)> "ゲットワイルド"
"ゲットワイルド"
iex(15)>

シングルクォーテーションで囲った場合

iex(15)> 'ゲットワイルド'
[12466, 12483, 12488, 12527, 12452, 12523, 12489]
iex(16)>

http://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html#char-lists
Elixirには文字リストというものが存在するらしく、文字列とは別物になるそう。

iex(19)> 'あいうえお'
[12354, 12356, 12358, 12360, 12362]
iex(20)>
iex(32)> to_string [12354, 12356, 12358, 12360, 12362]
"あいうえお"
iex(33)>
iex(43)> 'あいうえお' == "あいうえお"
false
iex(44)> 'あいうえお' == [12354, 12356, 12358, 12360, 12362]
true
iex(45)>

なんて馴染みの無い動きでしょう・・・。

匿名関数の実行

  • 匿名関数の定義
    • <> は文字列連結
iex(57)> kdm = fn a,b -> a <> b end
#Function<12.52032458/2 in :erl_eval.expr/5>
iex(58)>
  • 匿名関数の実行
    • ドットを付けないとエラーになる
iex(58)> kdm.("Get ", "Wild")
"Get Wild"
iex(59)>
iex(61)> kdm("Get ", "Wild")
** (CompileError) iex:61: undefined function kdm/2

iex(61)>

=(イコール)の扱い

= は代入ではなく、パターンマッチ。らしい。

iex(76)> getwild = 1
1
iex(77)>

これはgetwildに1を代入するではなく、getwildが1にマッチ(束縛)されたと考えるらしい。

iex(80)> 1 = getwild
1
iex(81)> 

なので、上のコードは成り立つ。
(1とgetwildはパターンマッチしている)

iex(81)> 2 = getwild
** (MatchError) no match of right hand side value: 1

iex(81)>

こっちはエラーになる。
(2とgetwildはパターンマッチしていないから)

同時に複数の束縛も可能。

iex(87)> {g,e,t,w,i,l,d} = {"ゲ","ッ","ト","ワ","イ","ル","ド"}
{"ゲ", "ッ", "ト", "ワ", "イ", "ル", "ド"}
iex(88)> {"ゲ","ッ","ト","ワ","イ","ル","ド"} = {g,e,t,w,i,l,d}
{"ゲ", "ッ", "ト", "ワ", "イ", "ル", "ド"}
iex(89)>

iex(91)> {"ゲ","ッ","ト","マ","イ","ル","ド"} = {g,e,t,w,i,l,d}
** (MatchError) no match of right hand side value: {"ゲ", "ッ", "ト", "ワ", "イ", "ル", "ド"}

iex(91)> g
"ゲ"
iex(92)> e
"ッ"
iex(93)> t
"ト"
iex(94)> w
"ワ"
iex(95)> i
"イ"
iex(96)> l
"ル"
iex(97)> d
"ド"
iex(98)>

変数同士で = を使うと再束縛になる

iex(162)> getwild = "ゲットワイルド"
"ゲットワイルド"
iex(163)> getmild = "ゲットマイルド"
"ゲットマイルド"
iex(164)> getwild = getmild
"ゲットマイルド"
iex(165)> getwild
"ゲットマイルド"
iex(166)>


iex(169)> getwild = "ゲットワイルド"
"ゲットワイルド"
iex(170)> getmild = "ゲットマイルド"
"ゲットマイルド"
iex(171)> getmild = getwild
"ゲットワイルド"
iex(172)> getmild
"ゲットワイルド"
iex(173)>

iex(176)> "ゲットマイルド" = getmild
** (MatchError) no match of right hand side value: "ゲットワイルド"

iex(176)>

パターンマッチの使い方例

例えばこんなモジュールと関数が定義されているとして・・・

iex(192)> defmodule SampleModule do
...(192)>
...(192)>   def do_something(status) do
...(192)>     case status do
...(192)>       {:ok, value} ->
...(192)>         "ok!bubbly! value is #{value}"
...(192)>       {:nope, value} ->
...(192)>         "nope... value is #{value}"
...(192)>       _ ->
...(192)>         "You are Get Wild"
...(192)>     end
...(192)>   end
...(192)>
...(192)> end
iex(211)> SampleMod.do_something({:ok, "kdm"})
"The status was :ok! value is kdm"
iex(212)>
nil
iex(213)> SampleMod.do_something({:nope, "kdm"})
"Nope nope nope nope... value is kdm"
iex(214)>


iex(216)> SampleMod.do_something(111)
"You are Get Wild"
iex(217)> SampleMod.do_something({:ok})
"You are Get Wild"
iex(218)> SampleMod.do_something({:nope})
"You are Get Wild"
iex(219)> SampleMod.do_something({"Get Wild?"})
"You are Get Wild"
iex(220)>

パターンマッチを利用して処理の分岐が可能。

関数のパターンマッチ

iex(242)> defmodule SampleModule do
...(242)>
...(242)>   def say_hello() do "Get Wild" end
...(242)>   def say_hello(name) do "Hello! #{name}." end
...(242)>
...(242)> end
  • 引数がある場合
iex(243)> SampleModule.say_hello("kdm")
"Hello! kdm."
iex(244)>
  • 引数が無い場合
iex(245)> SampleModule.say_hello()
"Get Wild"
iex(246)>

関数の中で if 引数がnilだったら・・・ みたいなことを記述しなくて済む。

mapの場合部分的なパターンマッチが可能

ex(276)> defmodule SampleModule do
...(276)>
...(276)>   def get_wild(%{"getwild" => wild}) do IO.puts wild end
...(276)>   def get_wild(_) do IO.puts "Not Wild" end
...(276)>
...(276)> end

iex(277)> params = %{"getwild" => "Get Wild", "hoge" => :hoge}
%{"getwild" => "Get Wild", "hoge" => :hoge}
iex(278)>

iex(279)> SampleModule.get_wild(params)
Get Wild
:ok

iex(280)> params = %{"kdm" => :hoge, "fuga" => :fuga}
%{"fuga" => :fuga, "kdm" => :hoge}
iex(281)>

iex(282)> SampleModule.get_wild(params)
Not Wild
:ok
iex(283)>

ifの書き方

色々な種類の書き方がある

iex(103)> if true do
...(103)> "Get " <> "Wild"
...(103)> end
"Get Wild"

iex(106)> if true, do: (
...(106)> "Get " <> "Wild"
...(106)> )
"Get Wild"

iex(109)> if true, do: "Get " <> "Wild"
"Get Wild"
iex(110)>

elseも同じ

iex(116)> if false do
...(116)> "Get Mild"
...(116)> else
...(116)> "Get Wild"
...(116)> end
"Get Wild"
iex(117)>

iex(120)> if false, do: "Get Mild", else: "Get Wild"
"Get Wild"
iex(121)>
4
4
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
4
4