Skip to content

Add Enum.sum/2 #11455

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
25 changes: 25 additions & 0 deletions lib/elixir/lib/enum.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3070,6 +3070,31 @@ defmodule Enum do
reduce(enumerable, 0, &+/2)
end

@doc """
Returns the sum of values returned by mapper.

Raises `ArithmeticError` if `mapper` returns a non-numeric value.

## Examples

iex> Enum.sum([1, 2, 3], &(&1 * 2))
12

iex> Enum.sum(1..10, &(&1 / 2))
27.5

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

iex> Enum.sum([{1}, {2}], &elem(&1, 0))
3

"""
@spec sum(t, (element -> number)) :: number
def sum(enumerable, mapper) do
reduce(enumerable, 0, fn elem, acc -> mapper.(elem) + acc end)
end

@doc """
Returns the product of all elements.

Expand Down
38 changes: 38 additions & 0 deletions lib/elixir/test/elixir/enum_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,44 @@ defmodule EnumTest do
end
end

describe "sum/2" do
test "with identity function behaves exactly the same as sum/1" do
id = &Function.identity/1

assert Enum.sum([], id) == 0
assert Enum.sum([1], id) == 1
assert Enum.sum([1, 2, 3], id) == 6
assert Enum.sum([1.1, 2.2, 3.3], id) == 6.6
assert Enum.sum([-3, -2, -1, 0, 1, 2, 3], id) == 0
assert Enum.sum(42..42, id) == 42
assert Enum.sum(11..17, id) == 98
assert Enum.sum(17..11, id) == 98
assert Enum.sum(11..-17, id) == Enum.sum(-17..11, id)

assert_raise ArithmeticError, fn ->
Enum.sum([{}], id)
end

assert_raise ArithmeticError, fn ->
Enum.sum([1, {}], id)
end
end

test "with mapping" do
assert Enum.sum([], & &1.foo) == 0
assert Enum.sum([%{foo: 1}], & &1.foo) == 1
assert Enum.sum([%{foo: 1, bar: 2}], & &1.foo) == 1

assert Enum.sum([1], &(&1 * 2)) == 2
assert Enum.sum([1, 2, 3], &(&1 * 2)) == 12
assert Enum.sum([1.1, 2.2, 3.3], &(&1 * 2)) == 13.2

assert_raise ArithmeticError, fn ->
Enum.sum([%{foo: nil}], & &1.foo) == 1
end
end
end

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