Skip to content

Instantly share code, notes, and snippets.

@oisdk
Last active August 29, 2015 14:24
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 oisdk/6fb4208afa18726c81fa to your computer and use it in GitHub Desktop.
Save oisdk/6fb4208afa18726c81fa to your computer and use it in GitHub Desktop.
public struct SelectGenerator<T> : GeneratorType {
private let vals: [T]
private var i: Int
mutating public func next() -> (T, [T])? {
guard ++i < vals.endIndex else { return nil }
var ar = vals
return (ar.removeAtIndex(i), ar)
}
public init(vals: [T]) {
self.i = -1
self.vals = vals
}
}
var g = SelectGenerator(vals: [1, 2, 3, 4])
g.next() // (1, [2, 3, 4])
g.next() // (2, [1, 3, 4])
g.next() // (3, [1, 2, 4])
g.next() // (4, [1, 2, 3])
g.next() // nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment