Created
August 23, 2023 04:08
-
-
Save dagronf/9030accf168016982ecce883361613a5 to your computer and use it in GitHub Desktop.
Zip two sequences together, padding the shorter sequences with nils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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