Skip to content

Instantly share code, notes, and snippets.

@dclelland
Created September 5, 2017 15:23
Show Gist options
  • Save dclelland/0afa6117a43c5c468a5f281eb0dda26e to your computer and use it in GitHub Desktop.
Save dclelland/0afa6117a43c5c468a5f281eb0dda26e to your computer and use it in GitHub Desktop.
public func zip<A>(_ arrays: [[A]]) -> ZipArrayCollection<A> {
return ZipArrayCollection(arrays)
}
public struct ZipArrayCollection<A>: Collection {
private let arrays: [[A]]
public let startIndex: Int
public let endIndex: Int
public init(_ arrays: [[A]]) {
self.arrays = arrays
self.startIndex = arrays.first?.startIndex ?? 0
self.endIndex = arrays.first?.endIndex ?? 0
}
public subscript(position: Int) -> [A] {
return arrays.map { array in
return array[position]
}
}
public func index(after index: Int) -> Int {
return index + 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment