I received a great question over email that I want to answer publicly via gist:
At the end [of the Grokking Lazy Sequences and Collections talk], someone asked you if they can just access a lazy collection randomly and it will not run the transform (map for example) on every element in the collection up to the one they chose.
You said that is correct, but I think that it only applies if the Collection is a RandomAccessCollection? Otherwise, access is not random and is sequential instead, so the iterator’s next() method will be called on each element up to the one requested, and that transform will be executed on each one.
Actually, my response is correct! Confusingly, Collection
does support subscript random access (with an expected O(1) runtime in fact) if you have an index. RandomAccessCollection
conformance implies that you can compute the distance between indicies in O(1) time (aka moving an index is fast).
If yo