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
19 stars 9 forks source link

eta-reduce lambdas in pipelines and function calls #307

Open jfmengels opened 5 months ago

jfmengels commented 5 months ago

What the rule should do:

Remove lambdas in pipelines and function calls when we we have:

-- |>
value
  |> fn1
  |> (\data -> fn2 x y z data)
-->
value
  |> fn1
  |> fn2 x y z

-- <|
(\data -> fn2 x y z data) <| fn1 <| value
-->
fn2 x y z <| fn1 <| value

I don't know whether we should simplify plain function calls. Probably?

(\data -> fn2 x y z data) (fn1 value)
-->
fn2 x y z (fn1 value)

  |> (\data -> fn2 x y z <| data)
-- should also be simplified to
  |> fn2 x y z

Things that should not be reported

-- More than one argument
  |> (\a b -> fn2 x y z a b)

-- Argument is referenced multiple times
  |> (\a -> fn2 x y z a a)

-- Argument is not the last argument
  |> (\a -> fn2 a x y z)

-- Argument is not given to a function
  |> (\a -> a / 2)

This package/rule is somewhat related, https://package.elm-lang.org/packages/jsuder-xx/elm-review-reducible-lambdas/latest/, but since it's more about reducing lambdas than reducing applications of anonymous functions.


I'm not entirely sure that this will always work well. But I think it's worth trying out. If the results are not great, then this could be extracted into a different rule.