Skip to content

Instantly share code, notes, and snippets.

@ralfebert
Created July 13, 2021 14:45
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 ralfebert/2b7b84185d547567ea5f9c7b9a9a5c68 to your computer and use it in GitHub Desktop.
Save ralfebert/2b7b84185d547567ea5f9c7b9a9a5c68 to your computer and use it in GitHub Desktop.
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