Skip to content

Commit b2e4005

Browse files
committed
Add specialized implementation for lists
1 parent 7644fd6 commit b2e4005

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
@@ -3492,6 +3492,12 @@ defmodule Enum do
34923492
"""
34933493
@doc since: "1.16.0"
34943494
@spec sum(t, (element -> number)) :: number
3495+
def sum(enumerable, fun)
3496+
3497+
def sum(list, fun) when is_list(list) and is_function(fun, 1) do
3498+
sum_list(list, fun, 0)
3499+
end
3500+
34953501
def sum(enumerable, fun) when is_function(fun, 1) do
34963502
reduce(enumerable, 0, fn x, acc -> acc + fun.(x) end)
34973503
end
@@ -4793,6 +4799,11 @@ defmodule Enum do
47934799
{:lists.reverse(acc), []}
47944800
end
47954801

4802+
## sum
4803+
4804+
defp sum_list([], _, acc), do: acc
4805+
defp sum_list([h | t], fun, acc), do: sum_list(t, fun, acc + fun.(h))
4806+
47964807
## take
47974808

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

0 commit comments

Comments
 (0)