Credence.Pattern.NoManualFrequencies
(credence v0.7.1)
Copy Markdown
Readability rule: Detects manual frequency counting with
Enum.reduce(list, %{}, fn x, acc -> Map.update(acc, KEY, 1, &(&1 + 1)) end).
When the counted key is the element itself, Enum.frequencies/1 does exactly
this in one call. When the key is a function of the element (e.g.
String.downcase(word)), Enum.frequencies_by/2 is the value-for-value
equivalent — the key expression is carried over verbatim, so both forms produce
byte-identical maps on every input (both available since Elixir 1.10).
Bad
list
|> Enum.reduce(%{}, fn item, counts ->
Map.update(counts, item, 1, &(&1 + 1))
end)
Enum.reduce(words, %{}, fn word, acc ->
Map.update(acc, String.downcase(word), 1, &(&1 + 1))
end)Good
Enum.frequencies(list)
Enum.frequencies_by(words, fn word -> String.downcase(word) end)