3 ms·
> IME there are three random features you might want on a collection: pick a single element, pick n elements and shuffle the collection. While it can't be #2 (a
by sdegutis 11y ago
> IME there are three random features you might want on a collection: pick a single element, pick n elements and shuffle the collection. While it can't be #2 (as that requires an elements count) the random() method could be either #1 or #3 and the naming provides no indication of which.
I don't see the confusion. #1 would be .random(), #2 would be .random(Int), and #3 would be .shuffle() with both mutating and non-mutating versions.
> So the naming is unhelpful and the only operation implemented is the least useful random collection operation.
There could be all three of these methods, which you could use depending on your needs.
> Finally you don't need to pull a whole library for that, it's a 3 lines extension.
The purpose of a standard library is to make common things easy. At least one of these three functions are found in nearly all other languages' standard libraries I've ever used.
- masklinn 11y ago> I don't see the confusion. #1 would be .random(), #2 would be .random(Int), and #3 would be .shuffle() with both mutating and non-mutating versions. All three operations are random, naming something "random" provides no indication whether it's a random indexing or a random shuffling. > There could be all three of these methods, which you could use depending on your needs. Sure, but there isn't, and the one operation which is provided is the one you can't use as basic building blocks for the other two. > The purpose of a standard library is to make common things easy. At least one of these three functions are found in nearly all other languages' standard libraries I've ever used. Your point being? I'm not saying the operation is useless, I'm saying the naming is confusing and out of 3 possible operations using that name, the least useful one was selected.