Skip to content

Instantly share code, notes, and snippets.

@erica
Created September 15, 2015 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erica/d2c5fa5f0f0bb5f68752 to your computer and use it in GitHub Desktop.
Save erica/d2c5fa5f0f0bb5f68752 to your computer and use it in GitHub Desktop.
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