Skip to content

Use Enum.sum_by/2 when appropriate #13685

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

Merged
merged 1 commit into from
Jun 22, 2024
Merged
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
14 changes: 6 additions & 8 deletions lib/elixir/lib/registry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1182,8 +1182,8 @@ defmodule Registry do
def count(registry) when is_atom(registry) do
case key_info!(registry) do
{_kind, partitions, nil} ->
Enum.reduce(0..(partitions - 1), 0, fn partition_index, acc ->
acc + safe_size(key_ets!(registry, partition_index))
Enum.sum_by(0..(partitions - 1), fn partition_index ->
safe_size(key_ets!(registry, partition_index))
end)

{_kind, 1, key_ets} ->
Expand Down Expand Up @@ -1258,9 +1258,8 @@ defmodule Registry do
:ets.select_count(key_ets, spec)

{:duplicate, partitions, _key_ets} ->
Enum.reduce(0..(partitions - 1), 0, fn partition_index, acc ->
count = :ets.select_count(key_ets!(registry, partition_index), spec)
acc + count
Enum.sum_by(0..(partitions - 1), fn partition_index ->
:ets.select_count(key_ets!(registry, partition_index), spec)
end)
end
end
Expand Down Expand Up @@ -1352,9 +1351,8 @@ defmodule Registry do

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

{_kind, 1, key_ets} ->
Expand Down
6 changes: 3 additions & 3 deletions lib/elixir/lib/string.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3046,10 +3046,10 @@ defmodule String do
end

defp bag_difference(bag1, bag2) do
Enum.reduce(bag1, 0, fn {char, count1}, sum ->
Enum.sum_by(bag1, fn {char, count1} ->
case bag2 do
%{^char => count2} -> sum + max(count1 - count2, 0)
%{} -> sum + count1
%{^char => count2} -> max(count1 - count2, 0)
%{} -> count1
end
end)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/elixir/lib/task.ex
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ defmodule Task do

iex> strings = ["long string", "longer string", "there are many of these"]
iex> stream = Task.async_stream(strings, fn text -> text |> String.codepoints() |> Enum.count() end)
iex> Enum.reduce(stream, 0, fn {:ok, num}, acc -> num + acc end)
iex> Enum.sum_by(stream, fn {:ok, num} -> num end)
47

See `async_stream/5` for discussion, options, and more examples.
Expand Down
13 changes: 5 additions & 8 deletions lib/elixir/test/elixir/registry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -982,14 +982,11 @@ defmodule Registry.Test do
end

defp sum_pid_entries(registry, partitions) do
Enum.map(0..(partitions - 1), &Module.concat(registry, "PIDPartition#{&1}"))
|> sum_ets_entries()
end

defp sum_ets_entries(table_names) do
table_names
|> Enum.map(&ets_entries/1)
|> Enum.sum()
Enum.sum_by(0..(partitions - 1), fn partition ->
registry
|> Module.concat("PIDPartition#{partition}")
|> ets_entries()
end)
end

defp ets_entries(table_name) do
Expand Down