ForgeCredoChecks.RejectMap (forge_credo_checks v0.3.0)

Copy Markdown View Source

Basics

This check is disabled by default.

Learn how to enable it via .credo.exs.

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

Explanation

Replace Enum.reject/2 |> Enum.map/2 with a comprehension.

Why

The pipe walks the list twice and allocates an intermediate list of surviving elements before mapping. A comprehension does both in one pass, preserves order naturally, and avoids the reduce + reverse anti-pattern.

How to fix (in order of preference)

Preferred: comprehension.

# BEFORE
things
|> Enum.reject(&drop?/1)
|> Enum.map(&transform/1)

# AFTER
for x <- things, not drop?(x), do: transform(x)

One pass, in-order, no intermediate list, no reverse step.

Last resort: Enum.reduce/3. Only when a comprehension is awkward and the consumer does not care about order:

Enum.reduce(things, [], fn x, acc ->
  if drop?(x), do: acc, else: [transform(x) | acc]
end)

What NOT to do

Do not switch to Enum.reduce/3 and append |> Enum.reverse/1 to restore order. That second pass is exactly the cost the comprehension exists to avoid.

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.