Skip to content

Commit 570887c

Browse files
committed
Rename to sum_by
1 parent 3682c2e commit 570887c

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

lib/elixir/lib/enum.ex

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3480,30 +3480,30 @@ defmodule Enum do
34803480
34813481
## Examples
34823482
3483-
iex> Enum.sum([%{count: 1}, %{count: 2}, %{count: 3}], fn x -> x.count end)
3483+
iex> Enum.sum_by([%{count: 1}, %{count: 2}, %{count: 3}], fn x -> x.count end)
34843484
6
34853485
3486-
iex> Enum.sum(1..3, fn x -> x ** 2 end)
3486+
iex> Enum.sum_by(1..3, fn x -> x ** 2 end)
34873487
14
34883488
3489-
iex> Enum.sum([], fn x -> x.count end)
3489+
iex> Enum.sum_by([], fn x -> x.count end)
34903490
0
34913491
34923492
Filtering can be achieved by returning `0` to remove elements:
34933493
3494-
iex> Enum.sum([1, -2, 3], fn x -> if x > 0, do: x, else: 0 end)
3494+
iex> Enum.sum_by([1, -2, 3], fn x -> if x > 0, do: x, else: 0 end)
34953495
4
34963496
34973497
"""
34983498
@doc since: "1.16.0"
3499-
@spec sum(t, (element -> number)) :: number
3500-
def sum(enumerable, fun)
3499+
@spec sum_by(t, (element -> number)) :: number
3500+
def sum_by(enumerable, fun)
35013501

3502-
def sum(list, fun) when is_list(list) and is_function(fun, 1) do
3503-
sum_list(list, fun, 0)
3502+
def sum_by(list, fun) when is_list(list) and is_function(fun, 1) do
3503+
sum_by_list(list, fun, 0)
35043504
end
35053505

3506-
def sum(enumerable, fun) when is_function(fun, 1) do
3506+
def sum_by(enumerable, fun) when is_function(fun, 1) do
35073507
reduce(enumerable, 0, fn x, acc -> acc + fun.(x) end)
35083508
end
35093509

@@ -4804,10 +4804,10 @@ defmodule Enum do
48044804
{:lists.reverse(acc), []}
48054805
end
48064806

4807-
## sum
4807+
## sum_by
48084808

4809-
defp sum_list([], _, acc), do: acc
4810-
defp sum_list([h | t], fun, acc), do: sum_list(t, fun, acc + fun.(h))
4809+
defp sum_by_list([], _, acc), do: acc
4810+
defp sum_by_list([h | t], fun, acc), do: sum_by_list(t, fun, acc + fun.(h))
48114811

48124812
## take
48134813

lib/elixir/test/elixir/enum_test.exs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,21 +1321,21 @@ defmodule EnumTest do
13211321
end
13221322
end
13231323

1324-
test "sum/2" do
1325-
assert Enum.sum([], &hd/1) == 0
1326-
assert Enum.sum([[1]], &hd/1) == 1
1327-
assert Enum.sum([[1], [2], [3]], &hd/1) == 6
1328-
assert Enum.sum([[1.1], [2.2], [3.3]], &hd/1) == 6.6
1329-
assert Enum.sum([[-3], [-2], [-1], [0], [1], [2], [3]], &hd/1) == 0
1324+
test "sum_by/2" do
1325+
assert Enum.sum_by([], &hd/1) == 0
1326+
assert Enum.sum_by([[1]], &hd/1) == 1
1327+
assert Enum.sum_by([[1], [2], [3]], &hd/1) == 6
1328+
assert Enum.sum_by([[1.1], [2.2], [3.3]], &hd/1) == 6.6
1329+
assert Enum.sum_by([[-3], [-2], [-1], [0], [1], [2], [3]], &hd/1) == 0
13301330

1331-
assert Enum.sum(1..3, &(&1 ** 2)) == 14
1331+
assert Enum.sum_by(1..3, &(&1 ** 2)) == 14
13321332

13331333
assert_raise ArithmeticError, fn ->
1334-
Enum.sum([[{}]], &hd/1)
1334+
Enum.sum_by([[{}]], &hd/1)
13351335
end
13361336

13371337
assert_raise ArithmeticError, fn ->
1338-
Enum.sum([[1], [{}]], &hd/1)
1338+
Enum.sum_by([[1], [{}]], &hd/1)
13391339
end
13401340
end
13411341

0 commit comments

Comments
 (0)