jfmengels/elm-review-simplify

More simplifications for List.take and List.drop

Open

#268 opened on Oct 9, 2023

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

Repository metrics

Stars
 (22 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

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 in List.take n list)

List.drop with negative n - ✅ #270

List.drop -1 list
--> list

Literal lists with size <= n (drop: ✅ #270)

List.take 5 [a,b,c]
--> [a,b,c]

List.drop 5 [a,b,c]
--> []

List.take 1 (List.singleton x)
--> List.singleton x

List.drop 1 (List.singleton x)
--> []

List.repeat with non-negative n <= repeat arg

List.take 3 (List.repeat 5 x)
--> List.repeat 3 x

List.drop 3 (List.repeat 5 x)
--> List.drop 2 x

List.range with size >= n

List.take 3 (List.range 0 10)
--> List.range 0 2

List.drop 3 (List.range 0 10)
--> List.range 3 10

Generalization question

Some of these can be generalized more, f.ex.

-- n > 0
List.drop n (List.repeat m x)
--> List.repeat (m - Basics.max 0 n) x

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.

Contributor guide