Open10

Elixir quiz

nil > 0

答え

true

[1 | [2 | [3 | []]]]

答え

[1, 2, 3]

[1, [2], [[3]]] |> List.flatten()

答え

[1, 2, 3]

[1, [2], [[3]]] |> Enum.flat_map(& &1)

答え

** (Protocol.UndefinedError) protocol Enumerable not implemented for 1 of type Integer

[[], [2], [[3]]] |> Enum.flat_map(& &1)

答え

[2, [3]]

defmodule Quiz do
  def which(0), do: "zero"
  def which(n) when n == 0, do: "guard zero"
  def which(n), do: n
end

Quiz.which(0.0)
答え

"guard zero"

0 == 0.0 #=> true
0 === 0.0 #=> false

what_is_this = if false, do: true

答え

nil

iex> %{c: 1, b: 2, a: 3}

答え

%{a: 3, b: 2, c: 1}

Map.take(%{a: 1, b: 2, c: 3}, [:c, :b, :a])

答え

%{a: 1, b: 2, c: 3}

Enum.into([a: 1, a: 2], %{})

答え

%{a: 2}

Regex.scan(~r/./, "abcde")

答え

[["a"], ["b"], ["c"], ["d"], ["e"]]

Regex.scan(~r/.*/, "abcde")

答え

[["abcde"], [""]]

Regex.scan(~r/.*?/, "abcde")

答え

[[""], ["a"], [""], ["b"], [""], ["c"], [""], ["d"], [""], ["e"], [""]]

map = %{a: 1, b: 2}
# 次の3つ
map.a
map[:c]
map.c
答え
1
nil
** (KeyError) key :c not found in: %{a: 1, b: 2}
%{map | c: 3}
答え
** (KeyError) key :c not found in: %{a: 1, b: 2}

10 / 2

答え

5.0

div(10, 2)

答え

5

div(10, 2.0)

答え

** (ArithmeticError) bad argument in arithmetic expression

"My name is " <> <<what::binary>> = "My name is Hoge"
答え

"Hoge"

<<what::binary>> <> " is Hoge" = "My name is Hoge"
答え

(CompileError)

<<what::binary-7>> <> " is Hoge" = "My name is Hoge"
答え

"My name"

ログインするとコメントできます