Skip to content

Instantly share code, notes, and snippets.

@oisdk
Created September 1, 2015 20:07
Show Gist options
  • Save oisdk/3a0157c900b647a365ea to your computer and use it in GitHub Desktop.
Save oisdk/3a0157c900b647a365ea to your computer and use it in GitHub Desktop.
struct WithIndexGenerator<Base : CollectionType> : GeneratorType {
private let base: Base
private var index: Base.Index
mutating func next() -> (Base.Index, Base.Generator.Element)? {
return index == base.endIndex ? nil : (index, base[index++])
}
}
struct WithIndexSequence<Base : CollectionType> : SequenceType {
private let base: Base
func generate() -> WithIndexGenerator<Base> {
return WithIndexGenerator(base: base, index: base.startIndex)
}
}
extension CollectionType {
func withIndices() -> WithIndexSequence<Self> {
return WithIndexSequence(base: self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment