6} -> ...> "This clause won't match" ...> {1, x, 3} -> ...> "This clause will match and bind x to 2 in this clause" ...> _ -> ...> "This clause would match any value" ...> end ɾϚονͨ͠અ͕࣮ߦ͞ΕΔ ɾϚονͰଋറ͞ΕͨมͦͷઅͰͷΈ༻Մೳ
iex> Blank.blank?(%{}) true iex> Blank.blank?([]) true defprotocol Blank do def blank?(data) end defimpl Blank, for: Map do def blank?(map) do map_size(map) == 0 end end defimpl Blank, for: List do def blank?([]), do: true def blank?(_), do: false end ɾϙϦϞʔϑΟζϜΛ࣮ݱ͢ΔͨΊͷͷ ɾEFGQSPUPDPMͰϓϩτίϧΛఆٛ ɾEFpNQMͰϓϩτίϧΛ࣮
%User{age: 27, name: "john"} iex> Blank.blank?(%User{}) false defprotocol Blank do def blank?(data) end defimpl Blank, for: User do def blank?(_), do: false end ɾϢʔβఆٛͷ4USVDUʹରͯ͠1SPUPDPMΛ࣮Մೳ
do a + b end end $ iex -S mix Erlang/OTP 17 [erts-6.2] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async- threads:10] [hipe] [kernel-poll:false] [dtrace] Interactive Elixir (1.1.0-dev) - press Ctrl+C to exit (type h() ENTER for help) iex(1)> Spam.sum(1, 2) 3
"the truth" do assert Spam.sum(1, 2) == 3 end end $ mix test Compiled lib/spam.ex Generated spam.app . Finished in 0.03 seconds (0.03s on load, 0.00s on tests) 1 tests, 0 failures Randomized with seed 45042 ɾ&Y6OJUͱ͍͏ϢχοτςετϥΠϒϥϦ͕ඪ४ఴ
"the truth" do assert Spam.sum(1, 2) == 2 end end $ mix test 1) test the truth (SpamTest) test/spam_test.exs:4 Assertion with == failed code: Spam.sum(1, 2) == 2 lhs: 3 rhs: 2 stacktrace: test/spam_test.exs:5 Finished in 0.03 seconds (0.03s on load, 0.00s on tests) 1 tests, 1 failures Randomized with seed 983758
def sum(a, b) do a + b end end $ mix compile $ mix dialyzer Starting Dialyzer dialyzer --no_check_plt --plt ~/.dialyxir_core_17_1.0.2.plt - Wunmatched_returns -Werror_handling -Wrace_conditions -Wunderspecs ./ebin Proceeding with analysis... spam.ex:2: Invalid type specification for function 'Elixir.Spam':sum/2. The success typing is (number(),number()) -> number() done in 0m0.88s done (warnings were emitted) ɾEJBMZ[FSEJBMZYJS IUUQTHJUIVCDPNKFSFNZKIEJBMZYJS Ͱ࣮ߦՄೳ
is the result of invoking `fun` on each corresponding item of `collection`. For dicts, the function expects a key-value tuple. ## Examples iex> Enum.map([1, 2, 3], fn(x) -> x * 2 end) [2, 4, 6] iex> Enum.map([a: 1, b: 2], fn({k, v}) -> {k, -v} end) [a: -1, b: -2] """ @spec map(t, (element -> any)) :: list def map...
collection, where each item is the result of invoking fun on each corresponding item of collection. For dicts, the function expects a key-value tuple. Examples ᴺ iex> Enum.map([1, 2, 3], fn(x) -> x * 2 end) ᴺ [2, 4, 6] ᴺ ᴺ iex> Enum.map([a: 1, b: 2], fn({k, v}) -> {k, -v} end) ᴺ [a: -1, b: -2]