Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.