[ruby-core:115244] [Ruby master Feature#19987] add sample method to Range

Issue #19987 has been reported by horv77@protonmail.com (Andras Horvath). ---------------------------------------- Feature #19987: add sample method to Range https://bugs.ruby-lang.org/issues/19987 * Author: horv77@protonmail.com (Andras Horvath) * Status: Open * Priority: Normal ---------------------------------------- Dear Devs, I'd like to suggest a change. Since the following works: (1..99).first(5) Therefore this one could be logical and useful to work: (1..99).sample(5) Thanks, Andras -- https://bugs.ruby-lang.org/

Issue #19987 has been updated by ufuk (Ufuk Kayserilioglu). The `first` call works because `Range` is an `Enumerable`, and since getting the first element is an act of enumeration. On the other hand, `sample` needs to know the full list to do what it is doing, so can't be seen as an act of enumeration. For example, `first` would work with an unbounded enumerable, but there is no way `sample` could: ```ruby (1..).first(5) # => [1, 2, 3, 4, 5] ``` ---------------------------------------- Feature #19987: add sample method to Range https://bugs.ruby-lang.org/issues/19987#change-105158 * Author: horv77@protonmail.com (Andras Horvath) * Status: Open * Priority: Normal ---------------------------------------- Dear Devs, I'd like to suggest a change. Since the following works: (1..99).first(5) Therefore this one could be logical and useful to work: (1..99).sample(5) Thanks, Andras -- https://bugs.ruby-lang.org/

Sounds logical, thank you. Andras Sent with Proton Mail secure email. On Sunday, November 5th, 2023 at 10:39 AM, ufuk (Ufuk Kayserilioglu) via ruby-core <ruby-core@ml.ruby-lang.org> wrote:
Issue #19987 has been updated by ufuk (Ufuk Kayserilioglu).
The `first` call works because `Range` is an `Enumerable`, and since getting the first element is an act of enumeration. On the other hand, `sample` needs to know the full list to do what it is doing, so can't be seen as an act of enumeration.
For example, `first` would work with an unbounded enumerable, but there is no way `sample` could: ```ruby (1..).first(5) # => [1, 2, 3, 4, 5]
```
---------------------------------------- Feature #19987: add sample method to Range https://bugs.ruby-lang.org/issues/19987#change-105158
* Author: horv77@protonmail.com (Andras Horvath) * Status: Open * Priority: Normal ---------------------------------------- Dear Devs, I'd like to suggest a change. Since the following works:
(1..99).first(5)
Therefore this one could be logical and useful to work:
(1..99).sample(5)
Thanks, Andras
-- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org...

Issue #19987 has been updated by rubyFeedback (robert heiler). I think conceptually .first() is not the same as .sample(). At the least when I read it, I would assume that .first() yields the first five elements (if the argument is 5), whereas .sample() should probably return 5 random elements. Not sure if others read it the same way, but that was my initial impression. ---------------------------------------- Feature #19987: add sample method to Range https://bugs.ruby-lang.org/issues/19987#change-105161 * Author: horv77@protonmail.com (Andras Horvath) * Status: Open * Priority: Normal ---------------------------------------- Dear Devs, I'd like to suggest a change. Since the following works: (1..99).first(5) Therefore this one could be logical and useful to work: (1..99).sample(5) Thanks, Andras -- https://bugs.ruby-lang.org/

Issue #19987 has been updated by nobu (Nobuyoshi Nakada). Status changed from Open to Feedback Even if limited to bound `Range`, we can't enumerate all possible elements in `Float`. As in the first proposal, it would make sense only for `Integer` ranges. May we close this? ---------------------------------------- Feature #19987: add sample method to Range https://bugs.ruby-lang.org/issues/19987#change-105165 * Author: horv77@protonmail.com (Andras Horvath) * Status: Feedback * Priority: Normal ---------------------------------------- Dear Devs, I'd like to suggest a change. Since the following works: (1..99).first(5) Therefore this one could be logical and useful to work: (1..99).sample(5) Thanks, Andras -- https://bugs.ruby-lang.org/

Issue #19987 has been updated by nobu (Nobuyoshi Nakada). Maybe what you want is `Random#rand` with a `Range`? ```ruby r = Random.new 5.times.map {r.rand(0..99)} #=> [83, 63, 43, 70, 90] ``` ---------------------------------------- Feature #19987: add sample method to Range https://bugs.ruby-lang.org/issues/19987#change-105168 * Author: horv77@protonmail.com (Andras Horvath) * Status: Feedback * Priority: Normal ---------------------------------------- Dear Devs, I'd like to suggest a change. Since the following works: (1..99).first(5) Therefore this one could be logical and useful to work: (1..99).sample(5) Thanks, Andras -- https://bugs.ruby-lang.org/
participants (5)
-
horv77
-
horv77@protonmail.com (Andras Horvath)
-
nobu (Nobuyoshi Nakada)
-
rubyFeedback (robert heiler)
-
ufuk (Ufuk Kayserilioglu)