jfmengels / elm-review-simplify

Provides elm-review rules to simplify your Elm code
https://package.elm-lang.org/packages/jfmengels/elm-review-simplify/latest/
BSD 3-Clause "New" or "Revised" License
20 stars 9 forks source link

Simplify to more optimized mapping operations #112

Open jfmengels opened 1 year ago

jfmengels commented 1 year ago

Example of things the rule would report:

list
  |> List.map f
  |> List.head
-->
list
  |> List.head
  |> Maybe.map f

List.head (List.map f list)
-->
Maybe.map f (List.head list)

Using List.take or List.drop:

list
  |> List.map f
  |> List.take n
-->
list
  |> List.take n
  |> List.map f

Same for Array.slice:

array
  |> Array.map f
  |> Array.slice m n
-->
array
  |> Array.slice m n
  |> Array.map f

Tuples:

tuple
  |> Tuple.mapFirst f
  |> Tuple.first
-->
tuple
  |> Tuple.first
  |> f
-- and similarly for second

For tuples, we could even notice unnecessary operations ✅

tuple
  |> Tuple.mapSecond f
  |> Tuple.first
-->
tuple
  |> Tuple.first
-- and similarly for second

If you can think of other operations where mapping (or similar operations) can be improved, please mention it!

I am looking for: