Использование Sequence.joined()
- путь, как указал Мартин. Вы можете подтвердить, что это действительно лениво, используя эти прокладки, которые я сделал:
struct PrintingSequence<S: Sequence>: Sequence {
let wrapped: S
init(_ wrapped: S) {
print("Making a sequence wrapping \(wrapped) of type \(S.self)")
self.wrapped = wrapped
}
func makeIterator() -> PrintingIterator<S.Iterator> {
return PrintingIterator(wrapped.makeIterator())
}
}
struct PrintingIterator<I: IteratorProtocol>: IteratorProtocol {
var wrapped: I
init(_ wrapped: I) {
print("\nMaking an iterator wrapping \(wrapped) of type \(I.self)")
self.wrapped = wrapped
}
mutating func next() -> I.Element? {
let result = self.wrapped.next()
print("Yielding \(result as Any)")
return result
}
}
let joinedSequence = [
PrintingSequence([1, 2, 3].lazy),
PrintingSequence([4, 5, 6].lazy)
].joined()
var joinedIterator = joinedSequence.makeIterator()
print("\nAbout to start the loop")
while let i = joinedIterator.next() {
print("\tloop \(i)")
}
Какие отпечатки:
Making a sequence wrapping LazySequence<Array<Int>>(_base: [1, 2, 3]) of type LazySequence<Array<Int>>
Making a sequence wrapping LazySequence<Array<Int>>(_base: [4, 5, 6]) of type LazySequence<Array<Int>>
About to start the loop
Making an iterator wrapping IndexingIterator<Array<Int>>(_elements: [1, 2, 3], _position: 0) of type IndexingIterator<Array<Int>>
Yielding Optional(1)
loop 1
Yielding Optional(2)
loop 2
Yielding Optional(3)
loop 3
Yielding nil
Making an iterator wrapping IndexingIterator<Array<Int>>(_elements: [4, 5, 6], _position: 0) of type IndexingIterator<Array<Int>>
Yielding Optional(4)
loop 4
Yielding Optional(5)
loop 5
Yielding Optional(6)
loop 6
Yielding nil