Last active
August 29, 2015 14:24
-
-
Save oisdk/6fb4208afa18726c81fa 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
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