jfmengels/elm-review-simplify

eta-reduce lambdas in pipelines and function calls

Open

#307 opened on Apr 30, 2024

View on GitHub
 (0 comments) (1 reaction) (0 assignees)Elm (10 forks)github user discovery
enhancementhelp wanted

Repository metrics

Stars
 (22 stars)
PR merge metrics
 (PR metrics pending)

Description

What the rule should do:

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

  • an anonymous function with a single argument being directly applied
  • the argument is at the last argument to another function call
  • the argument is referenced only once
-- |>
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

  • an anonymous function with a single argument being directly applied
-- 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.

Contributor guide