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
import Foundation | |
import SwiftUI | |
struct IndexedCollection<Base: RandomAccessCollection>: RandomAccessCollection { | |
typealias Index = Base.Index | |
typealias Element = (index: Index, element: Base.Element) | |
let base: Base | |
var startIndex: Index { self.base.startIndex } | |
var endIndex: Index { self.base.endIndex } | |
func index(after i: Index) -> Index { | |
self.base.index(after: i) | |
} | |
func index(before i: Index) -> Index { | |
self.base.index(before: i) | |
} | |
func index(_ i: Index, offsetBy distance: Int) -> Index { | |
self.base.index(i, offsetBy: distance) | |
} | |
subscript(position: Index) -> Element { | |
(index: position, element: self.base[position]) | |
} | |
} | |
extension RandomAccessCollection { | |
func indexed() -> IndexedCollection<Self> { | |
IndexedCollection(base: self) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment