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 List.singleton x to [ x ] #244

Closed jfmengels closed 9 months ago

jfmengels commented 9 months ago

This is a lot more idiomatic Elm code.

List.singleton x
--> [ x ]

I don't know if we should so in the case of pipelines

f
  |> g
  |> h
  |> List.singleton
-->
[ f
    |> g
    |> h
]

but I feel like it will end up bringing more clarity to the code maybe? I've had similar push-backy thoughts on other simplifications (List.map f [ x ] --> [ f x ]) but in the end the results did feel nicer, so I think we should do it.

What do you think?

lue-bird commented 9 months ago

IMO this is subjective and code style related, not removing complexity/simplifying.

document : Browser.Document
document =
    { ... body =
        some
            |> ui
            |> Element.layout []
            |> List.singleton
    }

which is conceptually different in my mind compared to List.map f [ x ] --> [ f x ] which removes complexity (operate on data directly, not on the wrapped data). In my mind, this exactly like the debate about always vs \_ ->: Something that is best configured in a separate rule if you have a particular preference.

I do not feel as strongly about the case where List.singleton is applied, not piped, but fixing only that case will feel inconsistent and random, so I'd prefer not to add it.