Skip to content

Commit 4bde3c8

Browse files
author
José Valim
committed
Remove empty? from the Enum protocol, closes #1097
1 parent 9b0353a commit 4bde3c8

File tree

3 files changed

+23
-34
lines changed

3 files changed

+23
-34
lines changed

lib/elixir/lib/enum.ex

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ defprotocol Enum.Iterator do
4848
"""
4949
def iterator(collection)
5050

51-
@doc """
52-
The function used to check if the collection is empty.
53-
"""
54-
def empty?(collection)
55-
5651
@doc """
5752
The function used to check if a value exists within the collection.
5853
"""
@@ -87,22 +82,6 @@ defmodule Enum do
8782
@type index :: non_neg_integer
8883
@type default :: any
8984

90-
@doc """
91-
Returns true if the `collection` is empty, otherwise false.
92-
93-
## Examples
94-
95-
iex> Enum.empty?([])
96-
true
97-
iex> Enum.empty?([1,2,3])
98-
false
99-
100-
"""
101-
@spec empty?(t) :: boolean
102-
def empty?(collection) do
103-
I.empty?(collection)
104-
end
105-
10685
@doc """
10786
Checks if the `value` exists within the `collection`.
10887
@@ -347,6 +326,29 @@ defmodule Enum do
347326
end
348327
end
349328

329+
@doc """
330+
Returns true if the collection is empty, otherwise false.
331+
332+
## Examples
333+
334+
iex> Enum.empty?([])
335+
true
336+
iex> Enum.empty?([1,2,3])
337+
false
338+
339+
"""
340+
@spec empty?(t) :: boolean
341+
def empty?(collection) when is_list(collection) do
342+
collection == []
343+
end
344+
345+
def empty?(collection) do
346+
case I.iterator(collection) do
347+
{ _iterator, pointer } -> pointer == :stop
348+
list when is_list(list) -> list == []
349+
end
350+
end
351+
350352
@doc """
351353
Returns true if the first collection is equal to the second, every element in
352354
both collections is iterated through, as soon as an element differs, it
@@ -2161,9 +2163,6 @@ end
21612163
defimpl Enum.Iterator, for: List do
21622164
def iterator(list), do: list
21632165

2164-
def empty?([]), do: true
2165-
def empty?(_), do: false
2166-
21672166
def member?([], _), do: false
21682167
def member?(list, value), do: :lists.member(value, list)
21692168

@@ -2176,11 +2175,6 @@ defimpl Enum.Iterator, for: Function do
21762175
{ iterator, iterator.(first) }
21772176
end
21782177

2179-
def empty?(function) do
2180-
{ _iterator, first } = function.()
2181-
first == :stop
2182-
end
2183-
21842178
def member?(function, value) do
21852179
{ iterator, first } = function.()
21862180
do_member?(iterator.(first), iterator, value)

lib/elixir/lib/hash_dict.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,6 @@ end
580580

581581
defimpl Enum.Iterator, for: HashDict do
582582
def iterator(dict), do: HashDict.to_list(dict)
583-
def empty?(dict), do: HashDict.size(dict) == 0
584583
def member?(dict, { k, v }), do: match?({ :ok, ^v }, HashDict.fetch(dict, k))
585584
def member?(_dict, _), do: false
586585
def count(dict), do: HashDict.size(dict)

lib/elixir/lib/range.ex

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ defimpl Enum.Iterator, for: Range do
2626
{ iterator, iterator.(first) }
2727
end
2828

29-
def empty?(Range[first: first] = range) do
30-
Range.Iterator.count(first, range) < 1
31-
end
32-
3329
def member?(Range[first: first, last: last], value) do
3430
value in first..last
3531
end

0 commit comments

Comments
 (0)