@@ -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
21612163defimpl 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 )
0 commit comments