Skip to content

Commit 9b38e4e

Browse files
committed
Remove Range.empty?/1
1 parent 2d40ad0 commit 9b38e4e

File tree

3 files changed

+22
-38
lines changed

3 files changed

+22
-38
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ As of Elixir v1.12, implicitly decreasing ranges are soft-deprecated and warning
8484

8585
## Additional functions
8686

87-
Elixir v1.12 has the additional of many functions across the standard library. The `Enum` module received additions such as `Enum.count_until/2`, `Enum.product/1`, `Enum.zip_with/2`, and more. The `Integer` module now includes `Integer.pow/2` and `Integer.extended_gcd/2`. The `Range` module now deals with stepped ranges and includes new convenience functions such as `Range.empty?/1` and `Range.size/1`. Finally, the `Kernel` module got two new functions, `Kernel.then/2` and `Kernel.tap/2`, which are specially useful in `|>` pipelines.
87+
Elixir v1.12 has the additional of many functions across the standard library. The `Enum` module received additions such as `Enum.count_until/2`, `Enum.product/1`, `Enum.zip_with/2`, and more. The `Integer` module now includes `Integer.pow/2` and `Integer.extended_gcd/2`. Finally, the `Kernel` module got two new functions, `Kernel.then/2` and `Kernel.tap/2`, which are specially useful in `|>` pipelines.
8888

8989
## v1.12.0-dev
9090

@@ -123,7 +123,7 @@ Elixir v1.12 has the additional of many functions across the standard library. T
123123
* [Module] Add `Module.get_definition/2` and `Module.delete_definition/2`
124124
* [Module] Allow `@on_load` to be a private function
125125
* [Module] Validate `@dialyzer` related module attributes
126-
* [Range] Add `Range.new/3`, `Range.empty?/1`, and `Range.size/1`
126+
* [Range] Add `Range.new/3` and `Range.size/1`
127127
* [Regex] Add offset option to `Regex.scan/3` and `Regex.run/3`
128128
* [Registry] Support `:compression` on `Registry` tables
129129
* [Stream] Add `Stream.zip_with/2` and `Stream.zip_with/3`

lib/elixir/lib/enum.ex

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2026,11 +2026,13 @@ defmodule Enum do
20262026
def min_max(enumerable, empty_fallback \\ fn -> raise Enum.EmptyError end)
20272027

20282028
def min_max(first..last//step = range, empty_fallback) when is_function(empty_fallback, 0) do
2029-
if Range.empty?(range) do
2030-
empty_fallback.()
2031-
else
2032-
last = last - rem(last - first, step)
2033-
{Kernel.min(first, last), Kernel.max(first, last)}
2029+
case Range.size(range) do
2030+
0 ->
2031+
empty_fallback.()
2032+
2033+
_ ->
2034+
last = last - rem(last - first, step)
2035+
{Kernel.min(first, last), Kernel.max(first, last)}
20342036
end
20352037
end
20362038

@@ -3611,15 +3613,17 @@ defmodule Enum do
36113613
end
36123614

36133615
defp aggregate(first..last//step = range, fun, empty) do
3614-
if Range.empty?(range) do
3615-
empty.()
3616-
else
3617-
last = last - rem(last - first, step)
3616+
case Range.size(range) do
3617+
0 ->
3618+
empty.()
36183619

3619-
case fun.(first, last) do
3620-
true -> first
3621-
false -> last
3622-
end
3620+
_ ->
3621+
last = last - rem(last - first, step)
3622+
3623+
case fun.(first, last) do
3624+
true -> first
3625+
false -> last
3626+
end
36233627
end
36243628
end
36253629

lib/elixir/lib/range.ex

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -150,26 +150,6 @@ defmodule Range do
150150
"non-zero integer, got: #{inspect(first)}..#{inspect(last)}//#{inspect(step)}"
151151
end
152152

153-
@doc """
154-
Checks if the range is empty.
155-
156-
## Examples
157-
158-
iex> Range.empty?(1..0//1)
159-
true
160-
iex> Range.empty?(0..1//-1)
161-
true
162-
iex> Range.empty?(1..0)
163-
false
164-
iex> Range.empty?(0..1)
165-
false
166-
167-
"""
168-
@doc since: "1.12.0"
169-
def empty?(first..last//step) when step > 0 and first > last, do: true
170-
def empty?(first..last//step) when step < 0 and first < last, do: true
171-
def empty?(_.._//_), do: false
172-
173153
@doc """
174154
Returns the size of the range.
175155
@@ -242,7 +222,7 @@ defmodule Range do
242222
@doc since: "1.8.0"
243223
@spec disjoint?(t, t) :: boolean
244224
def disjoint?(first1..last1//step1 = range1, first2..last2//step2 = range2) do
245-
if empty?(range1) or empty?(range2) do
225+
if size(range1) == 0 or size(range2) == 0 do
246226
true
247227
else
248228
{first1, last1, step1} = normalize(first1, last1, step1)
@@ -273,7 +253,7 @@ defmodule Range do
273253
end
274254
end
275255

276-
@compile inline: [normalize: 3, empty?: 1]
256+
@compile inline: [normalize: 3]
277257
defp normalize(first, last, step) when first > last, do: {last, first, -step}
278258
defp normalize(first, last, step), do: {first, last, step}
279259

@@ -309,7 +289,7 @@ defimpl Enumerable, for: Range do
309289

310290
def member?(first..last//step = range, value) when is_integer(value) do
311291
cond do
312-
Range.empty?(range) ->
292+
Range.size(range) == 0 ->
313293
{:ok, false}
314294

315295
first <= last ->

0 commit comments

Comments
 (0)