Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More simplifications for List.take and List.drop #268

Open
morteako opened this issue Oct 9, 2023 · 4 comments
Open

More simplifications for List.take and List.drop #268

morteako opened this issue Oct 9, 2023 · 4 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@morteako
Copy link
Contributor

morteako commented Oct 9, 2023

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.

@lue-bird
Copy link
Collaborator

lue-bird commented Oct 9, 2023

Nice.

Other than those nitpicks, looks good!

@morteako
Copy link
Contributor Author

  • 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

  • List.take 3 (List.repeat 5 x)
    --> List.repeat 2 x
    should be repeat 3

Fixed! Thanks

@jfmengels
Copy link
Owner

Nice suggestions 👍

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 😅

@morteako
Copy link
Contributor Author

@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.

@jfmengels jfmengels added enhancement New feature or request help wanted Extra attention is needed labels Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants