Open morteako opened 9 months ago
Nice.
List.length (List.take 10 list)
--> 10
doesn't work because list can have less than 10 elements.
The take and drop ones are already tracked in https://github.com/jfmengels/elm-review-simplify/issues/2
The take and drop after map are already tracked in https://github.com/jfmengels/elm-review-simplify/issues/112
List.take 3 (List.repeat 5 x)
--> List.repeat 2 x
should be repeat 3
Other than those nitpicks, looks good!
List.length (List.take 10 list) --> 10
doesn't work because list can have less than 10 elements.
Yes, of course. My bad. This was a bit of a hasty late suggestion. Removed
The take and drop ones are already tracked in Simplifications to add #2
The take and drop after map are already tracked in Simplify to more optimized mapping operations #112 Ah. Makes sense. I removed them from this issue
List.take 3 (List.repeat 5 x) --> List.repeat 2 x
should be repeat 3
Fixed! Thanks
Nice suggestions :+1:
Other things we could do:
List.drop 2 (x :: y :: z :: list)
--> z :: list
List.drop 2 (x :: list)
--> List.drop 1 list
List.take 2 (x :: list)
--> x :: List.take 1 list
@morteako Would you like to work on these simplifications? Asking before @lue-bird snatches them away :sweat_smile:
@jfmengels I will at least take a look at the first one , and try to include
List.drop 2 ([x,y,z] ++ list)
--> z :: list
I am not personally sure about
List.take 2 (x :: list)
--> x :: List.take 1 list
It should be better for performance, but i think maybe the resulting code is a bit more unclear.
More simplifications for List.take and List.drop
Some suggestions for List.take and List.drop simplifications. I think take and map are functions that are used a lot, so new rules would probably be applied in many codebases 🚀
(
n
is refering to the Int argument , as inList.take n list
)List.drop with negative n - ✅ #270
Literal lists with size <= n (drop: ✅ #270)
List.repeat with non-negative n <= repeat arg
List.range with size >= n
Generalization question
Some of these can be generalized more, f.ex.
But I am not sure about the clarity of the resulting code and also about how it will impact further simplifications. So I went with the simpler one. Open for suggestions
Feedback and tips welcome! And of course more suggestions and corrections.
I can probably look into implementing most of these myself.