Created
March 22, 2017 06:55
-
-
Save Leo7654/c104a1f2ddf1d13c8985e31030f4857b to your computer and use it in GitHub Desktop.
zip3 for swift
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
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