perl5i: ARRAY/HASH->pick to get a random element
The Perl Foundation
Status: Closed
Time to complete: 48 hrs
Mentors: Paul Johnson, Florian Ragwitz, Michael G Schwern
See perl5i: ARRAY/HASH->pick to get a random element
Pick N random elements out of a list or hash (in which case its the yet to be defined Pair). Modeled on Perl 6's pick. http://svn.pugscode.org/pugs/docs/Perl6/Spec/S32-setting-library/Containers.pod
method pick ( Int $number = 1 )
pick guarantees not to return the same element twice in a single call.
What should it return? If you just want to pick one it seems silly to return a list or array ref. But I don't want to make an exception for where $number = 1. Maybe $item = @array->pick_one.
If $number is greater than the size of the list, it returns the entire list, shuffled. $number = 0 returns an empty list.
Uploaded Work
| File name/URL | File size | Date submitted | |
|---|---|---|---|
| ARRAY.pm | 5.4 KB | November 26 2011 20:01 UTC | |
| pick.t | 354 bytes | November 27 2011 19:09 UTC | |
| perl5i.pm | 30.0 KB | November 27 2011 19:18 UTC | |
| https://github.com/schwern/perl5i/pull/216 | n/a | December 07 2011 01:25 UTC |
I would like to work on this task.
This task has been assigned to Prashan Dharmasena. You have 48 hours to complete this task, good luck!
Note that this task contains an open design question because the use case is split. Often you want to pick a single element, in which case it should return a scalar. But it can also return a list, in which case you must return a list... but that makes the single element case annoying.
Should it always return a list? Should it be split into two methods? Should it return a magic "parcel" that works as both a list and scalar? Pick one way to implement it and discuss on the issue.
So I added a method pick() that always returns a list, whether or not it is empty or only has one element, and a method pick_one that only returns one element.
The work on this task is ready to be reviewed.
Melange has detected that the deadline has passed and no more work can be submitted. The submitted work should be reviewed.
The deadline of the task has been extended with 0 days and 12 hours.
One of the mentors has sent this task back for more work. Talk to the mentor(s) assigned to this task to satisfy the requirements needed to complete this task, submit your work again and mark the task as complete once you re-submit your work.
The deadline of the task has been extended with 1 days and 0 hours.
There's problems with the chosen algorithm. It's inefficient and not guaranteed to terminate. Please see the discussion on Github and let's comment on it there. Work off of the issue/145 branch, I've committed your work and my fixups. https://github.com/schwern/perl5i/issues/145#issuecomment-2911193
The deadline of the task has been extended with 1 days and 0 hours.
Melange has detected that the final deadline has passed and it has reopened the task.
We let the deadline expire, but Prashan's work is still in review and revisions. Please nobody else but Prashan claim this task.
I would like to work on this task.
This task has been assigned to Prashan Dharmasena. You have 48 hours to complete this task, good luck!
Melange has detected that the initial deadline has passed and it has set the task status to ActionNeeded. The student has 24 hours to submit the work before the task is reopened and sent back to the pool for other students to claim.
Melange has detected that the final deadline has passed and it has reopened the task.
I would like to work on this task.
This task has been assigned to Prashan Dharmasena. You have 48 hours to complete this task, good luck!
The work on this task is ready to be reviewed.
Congratulations, this task has been completed successfully.