View Source Credo.Check.Refactor.FilterCount (Credo v1.7.8)

Basics

This check is enabled by default.

Learn how to disable it via .credo.exs.

This check has a base priority of high and works with any version of Elixir.

Explanation

Enum.count/2 is more efficient than Enum.filter/2 |> Enum.count/1.

This should be refactored:

[1, 2, 3, 4, 5]
|> Enum.filter(fn x -> rem(x, 3) == 0 end)
|> Enum.count()

to look like this:

Enum.count([1, 2, 3, 4, 5], fn x -> rem(x, 3) == 0 end)

The reason for this is performance, because the two separate calls to Enum.filter/2 and Enum.count/1 require two iterations whereas Enum.count/2 performs the same work in one pass.

Check-Specific Parameters

There are no specific parameters for this check.

General Parameters

Like with all checks, general params can be applied.

Parameters can be configured via the .credo.exs config file.