Skip to content

Instantly share code, notes, and snippets.

@dagronf
Created August 23, 2023 04:08
Show Gist options
  • Save dagronf/9030accf168016982ecce883361613a5 to your computer and use it in GitHub Desktop.
Save dagronf/9030accf168016982ecce883361613a5 to your computer and use it in GitHub Desktop.
Zip two sequences together, padding the shorter sequences with nils
/// Zip two sequences together, padding the shorter sequence with nil
func zipPadded<Value1, Value2>(_ z1: any Sequence<Value1>, _ z2: any Sequence<Value2>) -> [(Value1?, Value2?)] {
var v1 = z1.makeIterator() as any IteratorProtocol<Value1>
var v2 = z2.makeIterator() as any IteratorProtocol<Value2>
var vv1 = v1.next()
var vv2 = v2.next()
var result = [(Value1?, Value2?)]()
while vv1 != nil || vv2 != nil {
result.append((vv1, vv2))
vv1 = v1.next()
vv2 = v2.next()
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment