Skip to content

Instantly share code, notes, and snippets.

Created July 13, 2021 14:45
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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