Created
September 15, 2015 16:03
-
-
Save erica/d2c5fa5f0f0bb5f68752 to your computer and use it in GitHub Desktop.
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 CollectionType { | |
// Return a scrambled index generator | |
func scrambledIndexGenerator() -> AnyGenerator<Self.Index> { | |
var indices = Array(self.startIndex..<self.endIndex) | |
return anyGenerator { | |
if indices.count == 0 {return nil} | |
let nextIndex = arc4random_uniform(UInt32(indices.count)) | |
let nextItem = indices.removeAtIndex(Int(nextIndex)) | |
return nextItem | |
} | |
} | |
// Permutation generator | |
func permutatedGenerator() -> PermutationGenerator<Self,AnySequence<Self.Index>> { | |
return PermutationGenerator(elements: self, indices: AnySequence(self.scrambledIndexGenerator())) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment