[swift-evolution] [Proposal] Add zip2WithNilPadding function

Антон Миронов antonvmironov at gmail.com
Mon Jan 4 12:24:52 CST 2016


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

Thanks,
Anton Mironov

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160104/0648430d/attachment.html>


More information about the swift-evolution mailing list