Skip to content

Instantly share code, notes, and snippets.

@oisdk

oisdk/PaddedZip.Swift

Last active Aug 29, 2015
Embed
What would you like to do?
struct PaddedZipGenerator<G0: GeneratorType, G1: GeneratorType> : GeneratorType {
typealias E0 = G0.Element
typealias E1 = G1.Element
typealias Element = (E0?, E1?)
private var (g0, g1): (G0?, G1?)
mutating func next() -> PaddedZipGenerator.Element? {
let e0: E0? = g0?.next() ?? {g0 = nil; return nil}()
let e1: E1? = g1?.next() ?? {g1 = nil; return nil}()
return (e0 != nil || e1 != nil) ? (e0, e1) : nil
}
}
struct PaddedZip<S0: SequenceType, S1: SequenceType> : SequenceType {
typealias Generator = PaddedZipGenerator<S0.Generator, S1.Generator>
private let (s0, s1): (S0, S1)
func generate() -> PaddedZip.Generator {
return PaddedZipGenerator(g0: s0.generate(), g1: s1.generate())
}
}
func zipWithPadding<S0: SequenceType, S1: SequenceType>(s0: S0, _ s1: S1) -> PaddedZip<S0, S1> {
return PaddedZip(s0: s0, s1: s1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.