Perhaps you may move the loop body out into a function / closure:
func doSomething(_ y: Y) {
...
}
doSomething(extra)
dictionary.values.forEach(doSomething)
Below would probably also achieve what you want, but looks like overkill:
struct ArrayListIterator<T>: IteratorProtocol {
private var iterators: [Array<T>.Iterator]
init(_ arrays: [[T]]) {
iterators = arrays.map { $0.makeIterator() }
}
mutating func next() -> T? {
while !iterators.isEmpty {
if let element = iterators[0].next() {
return element
}
iterators.removeFirst()
}
return nil
}
}
struct ArrayList<T>: Sequence {
let arrays: [[T]]
func makeIterator() -> ArrayListIterator<T> {
ArrayListIterator(arrays)
}
}
for v in ArrayList(arrays: [["a", "b", "c"], ["d"], ["e", "f"]]) {
print(v, terminator: "")
}
print() // abcdef