Skip to content

Commit 67dece5

Browse files
committed
Add specialized implementation for lists
1 parent f720ff5 commit 67dece5

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

lib/elixir/lib/enum.ex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3468,6 +3468,12 @@ defmodule Enum do
34683468
"""
34693469
@doc since: "1.16.0"
34703470
@spec sum(t, (element -> number)) :: number
3471+
def sum(enumerable, fun)
3472+
3473+
def sum(list, fun) when is_list(list) and is_function(fun, 1) do
3474+
sum_list(list, fun, 0)
3475+
end
3476+
34713477
def sum(enumerable, fun) when is_function(fun, 1) do
34723478
reduce(enumerable, 0, fn x, acc -> acc + fun.(x) end)
34733479
end
@@ -4760,6 +4766,11 @@ defmodule Enum do
47604766
{:lists.reverse(acc), []}
47614767
end
47624768

4769+
## sum
4770+
4771+
defp sum_list([], _, acc), do: acc
4772+
defp sum_list([h | t], fun, acc), do: sum_list(t, fun, acc + fun.(h))
4773+
47634774
## take
47644775

47654776
defp take_list(_list, 0), do: []

0 commit comments

Comments
 (0)