Skip to content

Instantly share code, notes, and snippets.

@oisdk
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oisdk/d6e2874ff2779a46ea94 to your computer and use it in GitHub Desktop.
Save oisdk/d6e2874ff2779a46ea94 to your computer and use it in GitHub Desktop.
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