More Related Content Similar to Elixirだ 第1回強化版 前半 (20) Elixirだ 第1回強化版 前半1. 第1回 - 基礎だ 1 -
GMO Pepabo, Inc.
Joe Honzawa
2015/5/13 Elixir勉強会
Elixirだ
9. 型など
> integer
> float
> atom
> boolean
> binary
> string
> list
> tuple
> map
> range
> function
> keyword list
など
15. Binary
> <<1>>
> <<256>> == <<0>>
> <<1>> == <<1::size(8)>>
> <<1, 0>> == <<256::size(16)>>
> <<1::float>> ==
<<63,240,0,0,0,0,0,0>>
17. List
> 単方向リスト
> [1, 2, 3]
> [true, [1, 2, 3], “hey”]
> [1 | [2, 3]] == [1, 2, 3]
> [1, 2 | []] == [1, 2]
> 先頭への要素追加は高速
19. Map
> %{}
> %{“price”=> 3980}
> map = %{price: 100, amount: 2}
> map.price
> Map.get(map, :price)
> map = Map.put(map, :amount, 1)
21. 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)
22. Keyword List
> {key, val}なタプルのリスト
> kw = [{:age, 3}, … ]
> mapっぽく書いてよい
> kw == [age: 3, … ]
> Keyword.get(kw, :age) == 3
33. def
defmodule MyModule do
def print(arg) do
iikanjini_hyoji(arg)
end
defp iikanjini_hyoji(arg) do
IO.inspect arg
end
end
MyModule.print %{price: 298}
45. 使用
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
49. 賢く束縛
> [x, y] = [1, 2]
> x == 1
> y == 2
> {1, x, 3} = {1, 2, 3}
> x == 2
51. 代表的な
> {:ok, res} = File.read “a.txt”
> [head | tail] = [1, 2, 3, 4]
> head == 1
> tail == [2, 3, 4]
54. ピン止め
> x = 1
> [x, y, 3] = [2, 2, 3]
> x == 2, y == 2
> [^x, y, 3] = [2, 2, 3]
> MatchError
↑
再束縛されちゃってる
58. ガードで使える奴ら
> ==
> !=
> ===
> !==
> >
> <
> <=
> >=
> 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