Skip to content

Instantly share code, notes, and snippets.

@oisdk
Created June 18, 2015 17:43
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/1842566221076c34c115 to your computer and use it in GitHub Desktop.
Save oisdk/1842566221076c34c115 to your computer and use it in GitHub Desktop.
public struct CycleGen<C: CollectionType> : GeneratorType, LazySequenceType {
typealias Element = C.Generator.Element
typealias Generator = CycleGen<C>
private let inner: C
private var innerGen: C.Generator
private init(col: C) {
self.inner = col
self.innerGen = col.generate()
}
public func generate() -> Generator {
return self
}
public mutating func next() -> Element? {
for ;;innerGen = inner.generate() {
if let next = innerGen.next() {
return next
}
}
}
}
public extension CollectionType {
func cycle() -> CycleGen<Self> {
return CycleGen(col: self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment