Skip to content

Commit a2fe01d

Browse files
authored
Use Enum.sum_by/2 when appropriate (#13685)
1 parent ef81ded commit a2fe01d

File tree

4 files changed

+15
-20
lines changed

4 files changed

+15
-20
lines changed

lib/elixir/lib/registry.ex

+6-8
Original file line numberDiff line numberDiff line change
@@ -1182,8 +1182,8 @@ defmodule Registry do
11821182
def count(registry) when is_atom(registry) do
11831183
case key_info!(registry) do
11841184
{_kind, partitions, nil} ->
1185-
Enum.reduce(0..(partitions - 1), 0, fn partition_index, acc ->
1186-
acc + safe_size(key_ets!(registry, partition_index))
1185+
Enum.sum_by(0..(partitions - 1), fn partition_index ->
1186+
safe_size(key_ets!(registry, partition_index))
11871187
end)
11881188

11891189
{_kind, 1, key_ets} ->
@@ -1258,9 +1258,8 @@ defmodule Registry do
12581258
:ets.select_count(key_ets, spec)
12591259

12601260
{:duplicate, partitions, _key_ets} ->
1261-
Enum.reduce(0..(partitions - 1), 0, fn partition_index, acc ->
1262-
count = :ets.select_count(key_ets!(registry, partition_index), spec)
1263-
acc + count
1261+
Enum.sum_by(0..(partitions - 1), fn partition_index ->
1262+
:ets.select_count(key_ets!(registry, partition_index), spec)
12641263
end)
12651264
end
12661265
end
@@ -1352,9 +1351,8 @@ defmodule Registry do
13521351

13531352
case key_info!(registry) do
13541353
{_kind, partitions, nil} ->
1355-
Enum.reduce(0..(partitions - 1), 0, fn partition_index, acc ->
1356-
count = :ets.select_count(key_ets!(registry, partition_index), spec)
1357-
acc + count
1354+
Enum.sum_by(0..(partitions - 1), fn partition_index ->
1355+
:ets.select_count(key_ets!(registry, partition_index), spec)
13581356
end)
13591357

13601358
{_kind, 1, key_ets} ->

lib/elixir/lib/string.ex

+3-3
Original file line numberDiff line numberDiff line change
@@ -3046,10 +3046,10 @@ defmodule String do
30463046
end
30473047

30483048
defp bag_difference(bag1, bag2) do
3049-
Enum.reduce(bag1, 0, fn {char, count1}, sum ->
3049+
Enum.sum_by(bag1, fn {char, count1} ->
30503050
case bag2 do
3051-
%{^char => count2} -> sum + max(count1 - count2, 0)
3052-
%{} -> sum + count1
3051+
%{^char => count2} -> max(count1 - count2, 0)
3052+
%{} -> count1
30533053
end
30543054
end)
30553055
end

lib/elixir/lib/task.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ defmodule Task do
716716
717717
iex> strings = ["long string", "longer string", "there are many of these"]
718718
iex> stream = Task.async_stream(strings, fn text -> text |> String.codepoints() |> Enum.count() end)
719-
iex> Enum.reduce(stream, 0, fn {:ok, num}, acc -> num + acc end)
719+
iex> Enum.sum_by(stream, fn {:ok, num} -> num end)
720720
47
721721
722722
See `async_stream/5` for discussion, options, and more examples.

lib/elixir/test/elixir/registry_test.exs

+5-8
Original file line numberDiff line numberDiff line change
@@ -982,14 +982,11 @@ defmodule Registry.Test do
982982
end
983983

984984
defp sum_pid_entries(registry, partitions) do
985-
Enum.map(0..(partitions - 1), &Module.concat(registry, "PIDPartition#{&1}"))
986-
|> sum_ets_entries()
987-
end
988-
989-
defp sum_ets_entries(table_names) do
990-
table_names
991-
|> Enum.map(&ets_entries/1)
992-
|> Enum.sum()
985+
Enum.sum_by(0..(partitions - 1), fn partition ->
986+
registry
987+
|> Module.concat("PIDPartition#{partition}")
988+
|> ets_entries()
989+
end)
993990
end
994991

995992
defp ets_entries(table_name) do

0 commit comments

Comments
 (0)