@@ -48,11 +48,6 @@ defprotocol Enum.Iterator do
48
48
"""
49
49
def iterator ( collection )
50
50
51
- @ doc """
52
- The function used to check if the collection is empty.
53
- """
54
- def empty? ( collection )
55
-
56
51
@ doc """
57
52
The function used to check if a value exists within the collection.
58
53
"""
@@ -87,22 +82,6 @@ defmodule Enum do
87
82
@ type index :: non_neg_integer
88
83
@ type default :: any
89
84
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
-
106
85
@ doc """
107
86
Checks if the `value` exists within the `collection`.
108
87
@@ -347,6 +326,29 @@ defmodule Enum do
347
326
end
348
327
end
349
328
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
+
350
352
@ doc """
351
353
Returns true if the first collection is equal to the second, every element in
352
354
both collections is iterated through, as soon as an element differs, it
@@ -2161,9 +2163,6 @@ end
2161
2163
defimpl Enum.Iterator , for: List do
2162
2164
def iterator ( list ) , do: list
2163
2165
2164
- def empty? ( [ ] ) , do: true
2165
- def empty? ( _ ) , do: false
2166
-
2167
2166
def member? ( [ ] , _ ) , do: false
2168
2167
def member? ( list , value ) , do: :lists . member ( value , list )
2169
2168
@@ -2176,11 +2175,6 @@ defimpl Enum.Iterator, for: Function do
2176
2175
{ iterator , iterator . ( first ) }
2177
2176
end
2178
2177
2179
- def empty? ( function ) do
2180
- { _iterator , first } = function . ( )
2181
- first == :stop
2182
- end
2183
-
2184
2178
def member? ( function , value ) do
2185
2179
{ iterator , first } = function . ( )
2186
2180
do_member? ( iterator . ( first ) , iterator , value )
0 commit comments