Last active
February 8, 2019 01:16
-
-
Save SlaunchaMan/21fc0f57bdcec79f03ec5da1e3609ed8 to your computer and use it in GitHub Desktop.
I wanted a version of zip(_:_:) that padded the end of the shorter sequence with Optionals.
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
import Foundation | |
func paddedZip<T, U, Sequence1, Sequence2>( | |
_ sequence1: Sequence1, | |
_ sequence2: Sequence2 | |
) -> Zip2Sequence<[T?], [U?]> where | |
Sequence1 : Sequence, Sequence1.Element == T, | |
Sequence2 : Sequence, Sequence2.Element == U { | |
var array1: [T?] = [] | |
var array2: [U?] = [] | |
var iterator1 = sequence1.makeIterator() | |
var iterator2 = sequence2.makeIterator() | |
var shouldContinue = true | |
while shouldContinue { | |
switch (iterator1.next(), iterator2.next()) { | |
case (.some(let element1), .some(let element2)): | |
array1.append(.some(element1)) | |
array2.append(.some(element2)) | |
case (.some(let element1), .none): | |
array1.append(.some(element1)) | |
array2.append(.none) | |
case (.none, .some(let element2)): | |
array1.append(.none) | |
array2.append(.some(element2)) | |
case (.none, .none): | |
shouldContinue = false | |
} | |
} | |
return zip(array1, array2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment