apple / swift-algorithms

Commonly used sequence and collection algorithms for Swift
Apache License 2.0
5.97k stars 443 forks source link

[Feature Requested] Shuffle a collection #172

Closed el-hoshino closed 2 years ago

el-hoshino commented 2 years ago

Now we have randomSample method for collections to get multiple random elements of a specific number with orders shuffled. This behavior should make it easy to make a shuffle method, which don't have to take any parameters (or a generator, if needed) to shuffle the collection. Inside the shuffle method, we just have to call randomSample(count: self.count) and it should work as expected:

let aCollection = [1, 2, 3, 4, 5]
let shuffled = aCollection.shuffled()
// ↑ which should be identical with `aCollection.randomSample(count: aCollection.count)`
el-hoshino commented 2 years ago

Sorry, my bad, I didn't notice that we already have shuffled() method in Swift core library 😅