Skip to content

Instantly share code, notes, and snippets.

@moderateepheezy
Created April 15, 2017 14:28
Show Gist options
  • Save moderateepheezy/c967d3181b53a7ed24cddff9f395e52f to your computer and use it in GitHub Desktop.
Save moderateepheezy/c967d3181b53a7ed24cddff9f395e52f to your computer and use it in GitHub Desktop.
Shuffle and pair in group
extension Array {
func chunked(by chunkSize: Int) -> [[Element]] {
return stride(from: 0, to: self.count, by: chunkSize).map {
Array(self[$0 ..< Swift.min($0 + chunkSize, self.count)])
}
}
}
extension Array {
mutating func shuffle () {
for i in (0..<self.count).reversed() {
let ix1 = i
let ix2 = Int(arc4random_uniform(UInt32(i+1)))
(self[ix1], self[ix2]) = (self[ix2], self[ix1])
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment