[Proposal] Add zip2WithNilPadding function

(Anton Mironov) #1

Function zip constructs a sequence of pairs out of two sequences (Zip2Sequence). This sequence contains `min(sequence1.count, sequence2.count)` elements. I suggest to add sequence that contains `max(sequence1.count, sequence2.count)` elements (Zip2SequenceWithNilPadding).
For example:

let n = [2, 3, 5, 7, 11]
let s = ["two", "three", "five", "seven", "eleven", "thirteen"]
let regularZip = zip(n, s)
// regularZip: 2 => two, 3 => three, 5 => five, 7 => seven, 11 => eleven (5 items)
let proposedZip = zipWithNilPadding(n, s)
// proposedZip: 2 => two, 3 => three, 5 => five, 7 => seven, 11 => eleven, nil => thirteen (6 items)

I personally use zip to manage two sequences as one. Sometimes I do need to manage leftovers from longer sequence. In such case regular zip is not helpful.

About types. In oppose to Zip2Sequence that contains pairs of elements from each sequence (Sequence1.Generator.Element, Sequence2.Generator.Element), Zip2SequenceWithNilPadding will have to contain pairs of optional elements from each sequence (Sequence1.Generator.Element?, Sequence2.Generator.Element?).

Original idea from here: http://stackoverflow.com/questions/25153477/in-swift-i-would-like-to-join-two-sequences-in-to-a-sequence-of-tuples

Anton Mironov

Add a padded version of zip to complement the current zip