An iterator of iterators
Given a 'collection of collections' like [[1, 2], [], [3], [4, 5, 6]], how would you design an iterator that hands out elements one at a time in flat order (1, 2, 3, 4, 5, 6)? It has to work directly in for-in.
Given a 'collection of collections' like [[1, 2], [], [3], [4, 5, 6]], how would you design an iterator that hands out elements one at a time in flat order (1, 2, 3, 4, 5, 6)? It has to work directly in for-in.
A struct conforming to IteratorProtocol keeps the outer iterator and the current inner iterator as state, and next() pulls the next inner one when the current is exhausted. A Sequence wrapper's makeIterator() gives you for-in support.
SwiftMultiple choiceBasics
Which statements about a lazy var stored property are correct? Pick all that apply.
#lazy
SwiftShort answerBasics
Extend Array with a myCompactMap that behaves like the standard library's compactMap. How…
#generics
SwiftShort answerBasics
During code review you find the same logic overloaded per type: func larger(_ a: Int, _…
#generics
SwiftShort answerAdvanced
Suppose there were no question-mark optional syntax and no standard Optional. If you had…
#generics