Edit: The answer is: this is one of AnyIterator's uses—it is, itself, a Sequence!
public extension AnyIterator {
/// A single-pass version of a sequence,
/// whose iteration can be "paused", if not fully-consumed in one operation.
init<Sequence: Swift.Sequence>(pauseable sequence: Sequence)
where Sequence.Element == Element {
self.init( sequence.makeIterator() )
}
}
let upperBound = 5
let pauseableRange = AnyIterator(pauseable: 1...upperLimit)
for _ in pauseableRange.prefix(1) { }
func doNothin<NothinDoin>(_: NothinDoin) { }
pauseableRange.prefix(1).forEach(doNothin)
_ = pauseableRange.prefix(1).map(doNothin)
XCTAssertEqual(
Array(pauseableRange), Array(4...upperBound)
)
Ok, I see what you're trying to do. I don't know why you'd want to model it as Sequence instead of directly using IteratorProtocol.
If you own the type, maybe you can model Sequence as a class conforming to both IteratorProtocol and Sequence, and have makeIterator returns self. If you want to have both non-pausing and pausing versions, or you don't own the type, I couldn't think of a better way compared to your AnySequence.