Skip to content

Instantly share code, notes, and snippets.

@Leo7654
Created March 22, 2017 06:55
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 Leo7654/c104a1f2ddf1d13c8985e31030f4857b to your computer and use it in GitHub Desktop.
Save Leo7654/c104a1f2ddf1d13c8985e31030f4857b to your computer and use it in GitHub Desktop.
zip3 for swift
struct Zip3Generator<A: IteratorProtocol, B: IteratorProtocol, C: IteratorProtocol>: IteratorProtocol {
private var first: A
private var second: B
private var third: C
private var index = 0
init(_ first: A, _ second: B, _ third: C) {
self.first = first
self.second = second
self.third = third
}
mutating func next() -> (A.Element, B.Element, C.Element)? { // swiftlint:disable:this large_tuple
if let a = first.next(), let b = second.next(), let c = third.next() {
return (a, b, c)
}
return nil
}
}
func zip<A: Sequence, B: Sequence, C: Sequence>(_ a: A,_ b: B,_ c: C) -> IteratorSequence<Zip3Generator<A.Iterator, B.Iterator, C.Iterator>> {
return IteratorSequence(Zip3Generator(a.makeIterator(), b.makeIterator(), c.makeIterator()))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment