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

feat: Add an iterator selection function #51

Merged
merged 2 commits into from
Mar 3, 2023
Merged

feat: Add an iterator selection function #51

merged 2 commits into from
Mar 3, 2023

Conversation

notgull
Copy link
Member

@notgull notgull commented Feb 18, 2023

This PR adds an iterator selection function, choice(), that takes an ExactSizeIterator and chooses a random value from it. In addition, I reimplement functions like alphanumeric() using choice().

Right now, the API returns Option<I::Item>, since an iterator could be empty. However, it may be slightly more idiomatic to have it return I::Item and panic if given an empty/buggy iterator.

Copy link
Collaborator

@taiki-e taiki-e left a comment

Choose a reason for hiding this comment

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

Thanks!

rand also has similar methods that return Option, so I think this API is good as is.

rand's choose/choose_stable also supports iterators that do not implement ExactSizeIterator, but given that the imprecision of size_hint complicate the implementation, it seems reasonable that we support only ExactSizeIterator.

(P.S., I closed and reopened PR to trigger CI)

@notgull notgull merged commit c1cbe9b into master Mar 3, 2023
@notgull notgull deleted the notgull/choice branch March 3, 2023 17:07
@notgull notgull mentioned this pull request Apr 7, 2023
notgull added a commit that referenced this pull request Jun 9, 2023
- **Breaking:** Remove interior mutability from `Rng`. (#47)
- Add a `fork()` method. (#49)
- Add a `no_std` mode. (#50)
- Add an iterator selection function. (#51)
- Add a `choose_multiple()` function for sampling several elements from an iterator. (#55)
- Use the `getrandom` crate for seeding on WebAssembly targets if the `js` feature is enabled. (#60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants