Upload
joenoh
View
190
Download
1
Embed Size (px)
Citation preview
型など> integer > float > atom > boolean > binary > string > list
> tuple > map > range > function > keyword list
など
Binary> <<1>> > <<256>> == <<0>> > <<1>> == <<1::size(8)>> > <<1, 0>> == <<256::size(16)>> > <<1::float>> == <<63,240,0,0,0,0,0,0>>
List> 単方向リスト > [1, 2, 3] > [true, [1, 2, 3], “hey”] > [1 | [2, 3]] == [1, 2, 3] > [1, 2 | []] == [1, 2]
> 先頭への要素追加は高速
Map> %{} > %{“price”=> 3980} > map = %{price: 100, amount: 2}
> map.price > Map.get(map, :price)
> map = Map.put(map, :amount, 1)
Function>無名関数 > fn (x, y) -> x * y end > fun = &(&1 * &2) # キャプチャ記法 > fun.(3, 4) == 12 # call
>定義済み関数のキャプチャ > IOモジュールのputs関数(arity: 1) > Enum.each([1, 2], &IO.puts/1)
Keyword List> {key, val}なタプルのリスト > kw = [{:age, 3}, … ]
> mapっぽく書いてよい > kw == [age: 3, … ]
> Keyword.get(kw, :age) == 3
Keyword List> configとして使われることが多い
> 引数として渡すとき > 最後の引数ならば [] 省略可Keyword.keys([a: 1, b: 2]) Keyword.keys(a: 1, b: 2)
紹介した型など> integer > float > atom > boolean > binary > string > list
> tuple > map > range > function > keyword list
condcond do rem(y, 400) == 0 -> “Yes” rem(y, 100) == 0 -> “No” rem(y, 4) == 0 -> “Yes” true -> “No” end
defdefmodule MyModule do def print(arg) do iikanjini_hyoji(arg) end
defp iikanjini_hyoji(arg) do IO.inspect arg end end
MyModule.print %{price: 298}
慣習> ワーカ関数の名前はdo_xxx > モジュール名とファイル構造は対応させる > lib/my_module/math.ex > MyModule.Math
> 最も関心のあるものを第一引数に > 例外を投げる関数の名前はxxx! > 真偽値を返す関数の名前はxxx?
デフォルト引数defmodule MyModule do def say(word \\ “hi”) do IO.puts word end end
MyModule.say(“hello”) MyModule.say
importdefmodule MyModule do import Enum, only: [count: 1]
def kazoeru(list) do count(list) # Enum.count/1 end end
MyModule.kazoeru [1, 2, 3] MyModule.count [1, 2] → ❌
aliasdefmodule MyModule do alias Enum, as: E
def kazoeru(list) do E.count(list) # Enum.count/1 end end
MyModule.kazoeru [1, 2, 3]
定義defmodule User do defstruct name: “”, age: 0 end
> 初期値が全てnilならdefmodule User do defstruct [:name, :age] end
> 1モジュール1構造体
使用user = %User{} user = %User{name: "jack"} # 作成
user.name Map.get(user, :name) # 値の取得
# パターンマッチと更新 def hbd(user = %User{age: age}) do %User{user | age: age+1} end
ここまでのまとめ> defで関数を定義
> defmoduleでモジュールを定義
> 関数はモジュールの中で定義
> importで関数を取り込む
> aliasで別名をつける
> defstructで構造体を定義
実際的なcase File.read(“a.txt”) do {:ok, res} -> res {:error, :enoent} -> “oh it isn’t here” {:error, :eacces} -> “you can’t read it” _ -> “?” end
ピン止め> x = 1
> [x, y, 3] = [2, 2, 3] > x == 2, y == 2
> [^x, y, 3] = [2, 2, 3] > MatchError
↑ 再束縛されちゃってる
defでdefmodule MyModule do def sum(list) when is_list(list) do # 略 end
def sum(_arg) do IO.puts "the arg must be a list" end end
caseで
case the_number do x when x in [1, 2, 3] -> IO.puts "small" _ -> IO.puts "big or negative" end
ガードで使える奴ら> == > != > === > !== > > > < > <= > >= > and > or > not > !
> + > - > * > / > <> (左辺がリテラル) > ++ (左辺がリテラル) > in > is_atom/1 > is_binary/1 > is_bitstring/1 > is_boolean/1 > is_float/1 > is_function/1
> is_function/2 > is_integer/1 > is_list/1 > is_map/1 > is_nil/1 > is_number/1 > is_pid/1 > is_port/1 > is_reference/1 > is_tuple/1 > abs/1 > bit_size/1 > byte_size/1
> div/2 > elem2 > hd/1 > length/1 > map_size/1 > node/0 > node/1 > rem/2 > round/1 > self/0 > tl/1 > trunc/1 > tuple_size/1