Skip to content

Improve list.sample docs and implementation #837

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

Merged
merged 8 commits into from
Jun 12, 2025

Conversation

giacomocavalieri
Copy link
Member

A couple of improvements here:

  • I've changed the sample labels to be consistent with similar stuff like take and drop, this should also make it easier to see that it might not always return n elements
  • I've added a link to the algorithm L documentation
  • The reservoir building is more efficient as it's no longer using list.range and list.map2, but building it in one go
  • There was a bug in the sampling algorithm where it could end up taking the same item twice from the list so I've updated it and made the tests more specific

Copy link
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!!

@lpil
Copy link
Member

lpil commented Jun 11, 2025

Changelog plz

This makes it consistent with other "similar" functions like take and
drop. The second label also makes it clearer that it might return less
items than expected.
To make sure that the sample function doesn't take duplicate items from
the original list
@giacomocavalieri
Copy link
Member Author

Done!

Copy link
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

@lpil lpil merged commit 8c626a4 into gleam-lang:main Jun 12, 2025
7 checks passed
@giacomocavalieri giacomocavalieri deleted the sample-docs branch June 12, 2025 10:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants