[#77789] [Ruby trunk Feature#12012] Add Boolean method — prodis@...
Issue #12012 has been updated by Fernando Hamasaki de Amorim.
4 messages
2016/10/27
[ruby-core:77589] [Ruby trunk Feature#12775] Random subset of array
From:
transfire@...
Date:
2016-10-11 18:15:03 UTC
List:
ruby-core #77589
Issue #12775 has been updated by Thomas Sawyer.
This definition of `#sample` seems a bit limited. I know it aligns with the statical definition but it is very easy do another way: `shuffle.take(n)`. Also, the interface is a little odd because it can return an element or an Array. Maybe it would be better if it could return repeats.
[1,2,3,4].sample(10) => [1,2,4,3,3,4,2,1,2,3]
Then #sample could also take a block and return an enumerator when given no argument. To get one element it would be `sample.first`.
But if backward compatibility needs to be preserved, then add this functionality by another name.
P.S. It would be weird but #sample could use negative numbers to allow repeating.
----------------------------------------
Feature #12775: Random subset of array
https://bugs.ruby-lang.org/issues/12775#change-60859
* Author: Tsuyoshi Sawada
* Status: Feedback
* Priority: Normal
* Assignee:
----------------------------------------
I often see use cases to randomly sample a subset from an array like this:
```ruby
a = [3, 2, 6, 2, 6, 1]
a.sample(rand(a.length + 1)) # => [2, 6, 6, 3]
```
I request extending `Array#sample` to let it take an option (such as `:arbitrary`) for doing that:
```ruby
a.sample(:arbitrary) # => [2, 6, 6, 3]
```
or perhaps having an independent method (like `samples`) to do it.
```ruby
a.samples # => [2, 6, 6, 3]
```
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>