#79: zip with Swift Sequences

zip combines two sequences into pairs of matching elements.

let names = ["Ada", "Grace", "Linus"]
let scores = [10, 9, 8]

for (name, score) in zip(names, scores) {
    print(name, score)
}

Iteration stops as soon as the shorter sequence ends.

It is a simple tool, but very handy when you want parallel iteration without manual indexes.


I'm also on Twitter and GitHub.

Subscribe via RSS.