jfmengels/elm-review-simplify
在 GitHub 查看eta-reduce lambdas in pipelines and function calls
Open
#307 创建于 2024年4月30日
enhancementhelp wanted
仓库指标
- Star
- (22 star)
- PR 合并指标
- (PR 指标待抓取)
描述
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.