View Source Credo.Check.Refactor.RejectFilter (Credo v1.7.10)
Basics
This check is disabled by default.
Learn how to enable it via
.credo.exs
.
This check is tagged
:controversial
This means that this check is more opinionated than others and not for everyone's taste.
This check has a base priority of 0
and works with any version of Elixir.
Explanation
One Enum.filter/2
is more efficient than Enum.reject/2 |> Enum.filter/2
.
This should be refactored:
["a", "b", "c"]
|> Enum.reject(&String.contains?(&1, "x"))
|> Enum.filter(&String.contains?(&1, "a"))
to look like this:
Enum.filter(["a", "b", "c"], fn letter ->
!String.contains?(letter, "x") && String.contains?(letter, "a")
end)
The reason for this is performance, because the two calls to
Enum.reject/2
and Enum.filter/2
require two iterations whereas
doing the functions in the single Enum.filter/2
only requires one.
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.