Credence.Pattern.PreferEnumCount (credence v0.8.0)

Copy Markdown

Detects Enum.reduce/3 calls that count elements matching a predicate (using 0 as initial accumulator and if pred, do: acc + 1, else: acc as body) and rewrites them to the more concise Enum.count/2.

Bad

values
|> Enum.reduce(0, fn count, odd_count ->
  if rem(count, 2) == 1, do: odd_count + 1, else: odd_count
end)

Good

values
|> Enum.count(&(rem(&1, 2) == 1))