快速连接惰性序列?

时间:2019-06-12 13:04:05

标签: swift sequence

我希望能够做类似[1, 2, 3].lazy + [4, 5, 6].lazy而不是([1, 2, 3] + [4, 5, 6]).lazy的事情,因为假设的前一个操作是恒定的,而后者是线性的。

1 个答案:

答案 0 :(得分:1)

正如马丁指出的那样,使用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