Upload
joenoh
View
5.416
Download
3
Embed Size (px)
Citation preview
Types(?)> integer > float > atom > boolean > binary > string
> list > tuple > map > range > function など
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
>定義済み関数のキャプチャ > IOモジュールのputs関数(arity: 1) > Enum.each([1, 2], &IO.puts/1)
condcond do rem(y, 400) == 0 -> “Yes” rem(y, 100) == 0 -> “No” rem(y, 4) == 0 -> “Yes” true -> “No” end
実際的なcase File.read(“a.txt”) do {:ok, res} -> res {:error, :enoent} -> “oh it isn’t here” {:error, :eacces} -> “you can’t read it” _ -> “?” end
MatchError> {:ok, res} = File.read “a.txt”
> File.read/1 > {:ok, content} > {:error, reason}
> (MatchError) no match of right hand side value: {:error, :enoent}
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}
重要なスライドですdefmodule MyModule.Math do def sum(list), do: sum(list, 0)
defp sum([], acc), do: acc
defp sum([h|t], acc) do sum(t, h+acc) end end
慣習> ワーカ関数の名前は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
ガード式defmodule MyModule do def sum(list) when is_list(list) do sum(list, 0) end
# 略 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
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]
Hex> Rubyにおけるrubygems > パッケージマネージャ > https://hex.pm > ErlangのライブラリもOK > ファイル構成見てよしなにシュッと
Mix project> lib/please.ex > test/ > please_test.exs > test_helper.exs
> config/config.exs > mix.exs
lib/please.exdefmodule Please do def give_me(:sushi), do: “🍣” def give_me(:beer), do: “🍺” def give_me(_other), do: “❓” end
テストライブラリ> ExUnit > とりあえずこれ覚えるべき
> ShouldI > Bruce Tateさん作 > “One Experiment, Multiple Measurements” > 中身はExUnit
> ESpec
test/please_test.exsdefmodule PleaseTest do use ExUnit.Case alias Please, as: Plz
test “sushi” do assert Plz.give_me(:sushi) == ”🍣” end
test “neither sushi nor beer” do assert Plz.give_me(:hoge) == ”❓” end end
Redも見ておきましょうdefmodule PleaseTest do use ExUnit.Case alias Please, as: Plz
test “sushi” do assert Plz.give_me(:sushi) == ”🍕” end
test “neither sushi nor beer” do assert Plz.give_me(:hoge) == ”❓” end end