Created
August 3, 2019 17:30
-
-
Save cuckookernel/7146bc9c4c2e862d997d26496b82d6b4 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
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