-
Notifications
You must be signed in to change notification settings - Fork 17
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
Add randomElement #124
Comments
This seems like the kind of thing that should go into a random number library, not a collections library. Seems awfully specialized. What if you want to choose This seems to me just one of a variety of random sampling tasks that are useful. (Note that "shuffle" is also in this class, and that is provided in the random number library.) |
Given my comments about a version receiving the random/seed I understand your comments. I shouldn't have suggested that. However the simple version is very handy to have, at least these languages think so:
|
Adding the method to |
Adding Python to languages that have this feature in their standard library. |
@Ichoran while I agree this should belong to |
@BalmungSan - I didn't mention It's fine to add |
@Ichoran ah fair and apologies for the misunderstanding. BTW, my point was also that AFAIK this repo was not limited to only the collections framework, but the stdlib in general. And with plans for unfreezing the stdlib for future versions of Scala 3 I guess is great to re-activate the discussions here. |
Yeah, rather than using a default argument, I'd recommend I'll add something like this to my kse3 library, and hopefully will report back if I run into any problems. |
@BalmungSan - I implemented something for arrays which generalizes fine to It is live in kse3 maths 0.2.13, and I decorated the collections themselves with sample (in addition to putting it in the random number generator I wrote). No major hitches save for the usual ones when you want extension methods on collections (extension methods don't always understand the inheritance hierarchy). Here's a runnable example (using the variants that take a Anyway, no significant problems! I ended up using |
Hey,
I propose the method
randomElement
to be added toIterable
. This method returns a random element from the collection.On
Iterable
it can be (probably naively) implemented with:On
Seq
:A version receiving the Random/seed could also be added:
Or maybe the Random/seed could be an Option (this way there would no overloads):
I'm certainly not the right person to know which approach is better.
The text was updated successfully, but these errors were encountered: