List - get first item
Prepare lists
lists = [
[],
[nil],
[1],
[1, 2],
[1, 2, 3]
]
[[], [nil], [1], [1, 2], [1, 2, 3]]
Prepare test function
test_fun = fn do_test ->
for list <- lists do
try do
do_test.(list)
rescue
e -> inspect(e)
end
end
end
#Function<42.125776118/1 in :erl_eval.expr/6>
hd
test_fun.(fn list ->
hd(list)
end)
["%ArgumentError{message: \"errors were found at the given arguments:\\n\\n * 1st argument: not a nonempty list\\n\"}",
nil, 1, 1, 1]
List.first/1
test_fun.(fn list ->
List.first(list)
end)
[nil, nil, 1, 1, 1]
Enum.fetch/2
test_fun.(fn list ->
Enum.fetch(list, 0)
end)
[:error, {:ok, nil}, {:ok, 1}, {:ok, 1}, {:ok, 1}]
Enum.fetch!/2
test_fun.(fn list ->
Enum.fetch!(list, 0)
end)
["%Enum.OutOfBoundsError{message: \"out of bounds error\"}", nil, 1, 1, 1]
[first | _rest]
test_fun.(fn list ->
[first | _rest] = list
first
end)
["%MatchError{term: []}", nil, 1, 1, 1]
[first, , ]
test_fun.(fn list ->
[first, _, _] = list
first
end)
["%MatchError{term: []}", "%MatchError{term: [nil]}", "%MatchError{term: [1]}",
"%MatchError{term: [1, 2]}", 1]
Enum.at/2
test_fun.(fn list ->
Enum.at(list, 0)
end)
[nil, nil, 1, 1, 1]