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

Add Array.repeat simplifications #179

Closed jfmengels closed 11 months ago

jfmengels commented 11 months ago

Adds simplifications for Array.repeat #174

I also wanted to do

Array.repeat 1 f -- Only when the number is 1
--> Array.fromList [ x ]

but the existing checks (wrapperRepeatChecks) don't work for this type which doesn't have a Array.singleton function. @lue-bird if you can think of an easy way to add this, let me know, otherwise I'll leave it for later and focus on other issues first :smile:

lue-bird commented 11 months ago

Yeah, there's no smart mechanism to allow this. I'd just add a custom check.

jfmengels commented 11 months ago

I've been trying to simplify List.repeat 1 but I'm not sure about the simplifications outside of the simple List.repeat 1 x case.

For instance, the following solution doesn't feel like an improvement (It's surprising how odd it feels to not have a Array.singleton function...) :

List.repeat 1
--> List.singleton >> Array.fromList

Not sure what to do when seeing it in a pipeline either, like

x 
  |> f
  |> Array.repeat 1
  |> g
-->
[ x 
  |> f
]
  |> Array.fromList
  |> g

I'm thinking we should not replace List.repeat 1, and maybe only do the simple List.repeat 1 x and List.repeat 1 <| x cases.

lue-bird commented 11 months ago

And yeah those Array.fromList singleton cases all seem very ugly. Might just be best to always leave Array.repeat 1 as is than to apply this fix so inconsistently.