-
-
Notifications
You must be signed in to change notification settings - Fork 461
choose_multiple
should return Result or Option
#1620
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
Comments
You are talking about
Hence your suggestion would be a breaking change to both the API and the specification. I am not convinced that your motivation is sufficient. Also, while it is common in Python (and Matlab and many other scripting languages) to specify a length or matrix dimension in order to generate multiple samples, this is not common practice in languages like Rust where it is possible to sequentially generate multiple samples without (significant) overhead (especially when using a distribution object like |
Note: I don't consider the name |
Fair enough, but it would have been nice to have the compiler to encourage me to think about this, rather than simply hoping the user's read the documentation. I'm fairly certain that there's a significant overlap between people using the |
Fair point. Perhaps it should be renamed to Given that we also have variants of this method with |
Perhaps methods like On a tangential note, why are these methods for sampling without replacement named with |
I support renaming I'm also not convinced we should return a result or option, because returning |
But We could go with an abbreviated variant of @serhii-havrylov's suggestion: |
So, we have:
For iterators, the word "choose" intuitively makes sense to me: the RNG chooses one or multiple elements to keep and discards the rest. For sequences, the concept carries over well enough for single elements but is less clear for multiple elements. The word "sample" is perhaps more appropriate given that the operation does not modify the input sequence. I suggest some modifications (to
|
I just wanted to add my two cents as someone transitioning to Rust from Python. While I agree that Additionally, when writing the actual code, it can sometimes be immediately unclear from the code whether you're working with the iterator or the collection itself. This ambiguity can lead to confusion, especially for those familiar with Python's approach. |
Thanks for the feedback. So you're saying that it might make sense to:
In that case we get:
That's a little different from the above but maybe more consistent with iterators (which still have Edit: added |
I guess it's reasonable to stick with just one verb, "choose," rather than using two verbs like "choose" and "sample." You could use |
If we're talking grammar — 'choice' is a noun (or sometimes an adjective), not a verb. So it might make more sense to use We already have So maybe we could do this:
|
I mistakenly assumed that
choose_multiple
functions like Python'srandom.choices
, which performs sampling with replacement. This led to unexpected behaviour in the following Rust code:Here,
my_vec
contains significantly more than 15 elements, but sample ends up with only 15 elements. This behaviour can be surprising and may introduce bugs.Since
choose_multiple
performs sampling without replacement, it would make more sense for it to return aResult
orOption
to return error orNone
when the requested number of elements cannot be selected without replacement.The text was updated successfully, but these errors were encountered: