Skip to content

... |> Enum.filter(...) |> List.first() to Enum.find(..., ...) #242

@janpieper

Description

@janpieper

I just stumbled upon some code and I was wondering whether this could be something for styler to rewrite 🤔

movies
|> Enum.filter(&is_cool/1)
|> List.first()

to:

Enum.find(movies, &is_cool/1)

When using Enum.filter/2 followed by List.first/1 all items within the list are processed independently of whether the first would already be enough as it matched is_cool?/1. Replacing it with Enum.find/2 would stop the processing the items with the first match.

Even when List.first/3 is used, replacing it with Enum.find/3 would work.

movies
|> Enum.filter(&is_cool/1) # or Enum.reject/2
|> List.first(:not_found)

to:

Enum.find(movies, :not_found, &is_cool/1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions