Swift: How does zip () handle two collections of different sizes?

The zip () function takes two sequences and returns a sequence of tuples:

output[i] = (sequence1[i], sequence2[i]) 

However, sequences can potentially have different sizes. My question is how does Swift deal with this language?

The documents were completely useless.

It seems to me there are two possibilities (in Swift):

  • Stop at the end of the shortest
  • Stop at the end of the longest by filling out the default constructor or a predefined value for the shorter element type
+5
source share
1 answer

Swift uses the first option, the resulting sequence will have a length equal to the smaller of the two inputs .

For instance:

 let a: [Int] = [1, 2, 3] let b: [Int] = [4, 5, 6, 7] let c: [(Int, Int)] = zip(a, b) // [(1, 4), (2, 5), (3, 6)] 
+8
source

Source: https://habr.com/ru/post/1235931/


All Articles