Skip to content

Instantly share code, notes, and snippets.

@cuckookernel
Created August 3, 2019 17:30
Show Gist options
  • Save cuckookernel/7146bc9c4c2e862d997d26496b82d6b4 to your computer and use it in GitHub Desktop.
Save cuckookernel/7146bc9c4c2e862d997d26496b82d6b4 to your computer and use it in GitHub Desktop.
struct InfRepeater: Sequence {
// a sequence that loops infinitely over another sequence
typealias Iterator = InfRepeaterIter
typealias Element = Int
var arr: Array<Element>
func makeIterator() -> Iterator {
return InfRepeaterIter( i: 0, arr: arr )
}
}
struct InfRepeaterIter : IteratorProtocol {
typealias Element = Int
var i : Int = 0
var arr : Array<Element>
mutating func next( ) -> Int? {
let ret = self.arr[self.i]
self.i += 1
self.i %= self.arr.count
return ret
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment