Skip to content

Adds support for Enum.sum/2 #11381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions lib/elixir/lib/enum.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3217,19 +3217,30 @@ defmodule Enum do
iex> Enum.sum(1..10//2)
25

iex> Enum.sum(1..10, & &1 * 2)
209

iex> Enum.sum([%{num: 1}, %{num: 2}], & &1.num)
3

iex> Enum.sum(%{num_2: 2, num_3: 3}, fn {_, v} -> v end)
5
"""
@spec sum(t) :: number
def sum(enumerable)
@spec sum(t, (element -> number)) :: number
def sum(enumerable, fun \\ fn item -> item end)

def sum(first..last//step, fun) do
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably the bit I'm more concerned about, from my testing it seems like the approach of using the fun on the "edges" of the range and then coercing it back into a range, seems like the way to go. But I'm open to feedback on this 🙌

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will only work if the function is linear, which we cannot guarantee. For example, &:math.sin/1 wouldn’t work as callback.

first = fun.(first)
last = fun.(last)

def sum(first..last//step = range) do
range
first..last//step
|> Range.size()
|> Kernel.*(first + last - rem(last - first, step))
|> div(2)
end

def sum(enumerable) do
reduce(enumerable, 0, &+/2)
def sum(enumerable, fun) do
reduce(enumerable, 0, &(fun.(&1) + &2))
end

@doc """
Expand Down
33 changes: 33 additions & 0 deletions lib/elixir/test/elixir/enum_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,39 @@ defmodule EnumTest do
end
end

test "sum/2" do
assert Enum.sum(
[%{number: 1, not_number: "foo"}, %{number: 2, not_number: "bar"}],
& &1.number
) == 3

assert Enum.sum([%{number: 1}, %{number: 2}], &(&1.number * 2)) == 6
assert Enum.sum(%{num_1: 1, num_2: 2, num_3: 3}, fn {_, v} -> v end) == 6
assert Enum.sum(42..42, &(&1 * 2)) == 84
assert Enum.sum(42..42, &div(&1, 2)) == 21
assert Enum.sum(11..17, &(&1 * 2)) == 364
assert Enum.sum(11..17, &div(&1, 2)) == 26

assert_raise ArgumentError, fn ->
Enum.sum(11..17, &(&1 / 2))
end

assert_raise ArgumentError, fn ->
Enum.sum(42..42, &(&1 / 2))
end

assert_raise ArithmeticError, fn ->
Enum.sum(
[%{number: 1, not_number: "foo"}, %{number: 2, not_number: "bar"}],
& &1.not_number
)
end

assert_raise ArithmeticError, fn ->
Enum.sum(%{letter_a: "a", letter_b: "b", letter_c: "c"}, fn {_, v} -> v end)
end
end

test "product/1" do
assert Enum.product([]) == 1
assert Enum.product([1]) == 1
Expand Down