Skip to content

Commit 739c1b6

Browse files
author
José Valim
committed
Keep existing APIs for min/max/min_by/max_by
1 parent 3e1e6dd commit 739c1b6

File tree

1 file changed

+40
-31
lines changed

1 file changed

+40
-31
lines changed

lib/elixir/lib/enum.ex

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,6 +1530,11 @@ defmodule Enum do
15301530
{:lists.reverse(list), acc}
15311531
end
15321532

1533+
@doc false
1534+
def max(enumerable, empty_fallback) when is_function(empty_fallback, 0) do
1535+
max(enumerable, &>=/2, empty_fallback)
1536+
end
1537+
15331538
@doc """
15341539
Returns the maximal element in the `enumerable` according
15351540
to Erlang's term ordering.
@@ -1572,26 +1577,22 @@ defmodule Enum do
15721577
0
15731578
15741579
"""
1580+
@spec max(t, (() -> empty_result)) :: element | empty_result when empty_result: any
15751581
@spec max(t, (element, element -> boolean) | module(), (() -> empty_result)) ::
15761582
element | empty_result
15771583
when empty_result: any
15781584
def max(enumerable, sorter \\ &>=/2, empty_fallback \\ fn -> raise Enum.EmptyError end) do
1579-
{sorter, empty_fallback} = max_sort_fun(sorter, empty_fallback)
1580-
aggregate(enumerable, sorter, empty_fallback)
1585+
aggregate(enumerable, max_sort_fun(sorter), empty_fallback)
15811586
end
15821587

1583-
# TODO: Deprecate me on 1.14
1584-
defp max_sort_fun(empty_fallback, default_empty_fallback)
1585-
when is_function(empty_fallback, 0) and is_function(default_empty_fallback, 0),
1586-
do: {&>=/2, empty_fallback}
1588+
defp max_sort_fun(sorter) when is_function(sorter, 2), do: sorter
1589+
defp max_sort_fun(module) when is_atom(module), do: &(module.compare(&1, &2) != :lt)
15871590

1588-
defp max_sort_fun(sorter, empty_fallback)
1589-
when is_function(sorter, 2) and is_function(empty_fallback, 0),
1590-
do: {sorter, empty_fallback}
1591-
1592-
defp max_sort_fun(module, empty_fallback)
1593-
when is_atom(module) and is_function(empty_fallback, 0),
1594-
do: {&(module.compare(&1, &2) != :lt), empty_fallback}
1591+
@doc false
1592+
def max_by(enumerable, fun, empty_fallback)
1593+
when is_function(fun, 1) and is_function(empty_fallback, 0) do
1594+
max_by(enumerable, fun, &>=/2, empty_fallback)
1595+
end
15951596

15961597
@doc """
15971598
Returns the maximal element in the `enumerable` as calculated
@@ -1637,6 +1638,8 @@ defmodule Enum do
16371638
nil
16381639
16391640
"""
1641+
@spec max_by(t, (element -> any), (() -> empty_result)) :: element | empty_result
1642+
when empty_result: any
16401643
@spec max_by(
16411644
t,
16421645
(element -> any),
@@ -1646,8 +1649,7 @@ defmodule Enum do
16461649
when empty_result: any
16471650
def max_by(enumerable, fun, sorter \\ &>=/2, empty_fallback \\ fn -> raise Enum.EmptyError end)
16481651
when is_function(fun, 1) do
1649-
{sorter, empty_fallback} = max_sort_fun(sorter, empty_fallback)
1650-
aggregate_by(enumerable, fun, sorter, empty_fallback)
1652+
aggregate_by(enumerable, fun, max_sort_fun(sorter), empty_fallback)
16511653
end
16521654

16531655
@doc """
@@ -1690,6 +1692,11 @@ defmodule Enum do
16901692
end
16911693
end
16921694

1695+
@doc false
1696+
def min(enumerable, empty_fallback) when is_function(empty_fallback, 0) do
1697+
min(enumerable, &<=/2, empty_fallback)
1698+
end
1699+
16931700
@doc """
16941701
Returns the minimal element in the `enumerable` according
16951702
to Erlang's term ordering.
@@ -1728,30 +1735,26 @@ defmodule Enum do
17281735
Finally, if you don't want to raise on empty enumerables, you can pass
17291736
the empty fallback:
17301737
1731-
iex> Enum.min([], &<=/2, fn -> 0 end)
1738+
iex> Enum.min([], fn -> 0 end)
17321739
0
17331740
17341741
"""
1742+
@spec min(t, (() -> empty_result)) :: element | empty_result when empty_result: any
17351743
@spec min(t, (element, element -> boolean) | module(), (() -> empty_result)) ::
17361744
element | empty_result
17371745
when empty_result: any
17381746
def min(enumerable, sorter \\ &<=/2, empty_fallback \\ fn -> raise Enum.EmptyError end) do
1739-
{sorter, empty_fallback} = min_sort_fun(sorter, empty_fallback)
1740-
aggregate(enumerable, sorter, empty_fallback)
1747+
aggregate(enumerable, min_sort_fun(sorter), empty_fallback)
17411748
end
17421749

1743-
# TODO: Deprecate me on 1.14
1744-
defp min_sort_fun(empty_fallback, default_empty_fallback)
1745-
when is_function(empty_fallback, 0) and is_function(default_empty_fallback, 0),
1746-
do: {&<=/2, empty_fallback}
1750+
defp min_sort_fun(sorter) when is_function(sorter, 2), do: sorter
1751+
defp min_sort_fun(module) when is_atom(module), do: &(module.compare(&1, &2) != :gt)
17471752

1748-
defp min_sort_fun(sorter, empty_fallback)
1749-
when is_function(sorter, 2) and is_function(empty_fallback, 0),
1750-
do: {sorter, empty_fallback}
1751-
1752-
defp min_sort_fun(module, empty_fallback)
1753-
when is_atom(module) and is_function(empty_fallback, 0),
1754-
do: {&(module.compare(&1, &2) != :gt), empty_fallback}
1753+
@doc false
1754+
def min_by(enumerable, fun, empty_fallback)
1755+
when is_function(fun, 1) and is_function(empty_fallback, 0) do
1756+
min_by(enumerable, fun, &<=/2, empty_fallback)
1757+
end
17551758

17561759
@doc """
17571760
Returns the minimal element in the `enumerable` as calculated
@@ -1799,10 +1802,16 @@ defmodule Enum do
17991802
"""
18001803
@spec min_by(t, (element -> any), (() -> empty_result)) :: element | empty_result
18011804
when empty_result: any
1805+
@spec min_by(
1806+
t,
1807+
(element -> any),
1808+
(element, element -> boolean) | module(),
1809+
(() -> empty_result)
1810+
) :: element | empty_result
1811+
when empty_result: any
18021812
def min_by(enumerable, fun, sorter \\ &<=/2, empty_fallback \\ fn -> raise Enum.EmptyError end)
18031813
when is_function(fun, 1) do
1804-
{sorter, empty_fallback} = min_sort_fun(sorter, empty_fallback)
1805-
aggregate_by(enumerable, fun, sorter, empty_fallback)
1814+
aggregate_by(enumerable, fun, min_sort_fun(sorter), empty_fallback)
18061815
end
18071816

18081817
@doc """

0 commit comments

Comments
 (0)