Created
September 1, 2015 20:07
-
-
Save oisdk/3a0157c900b647a365ea to your computer and use it in GitHub Desktop.
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
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