Created
April 15, 2017 14:28
-
-
Save moderateepheezy/c967d3181b53a7ed24cddff9f395e52f to your computer and use it in GitHub Desktop.
Shuffle and pair in group
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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