-
-
Save hsavit1/0dddb4fe1ebe2cc55225 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // Similar to `enumerate` but provides the collection's index type | |
| // rather than an Int for the position | |
| public func iterate<C: CollectionType>(collection: C) -> SequenceOf<(C.Index, C.Generator.Element)> { | |
| var index = collection.startIndex | |
| // type-inference doesn't want to work without this | |
| return SequenceOf { _ -> GeneratorOf<(C.Index, C.Generator.Element)> in | |
| return GeneratorOf { | |
| if index == collection.endIndex { | |
| return nil | |
| } | |
| let previous = index++ | |
| return (previous, collection[previous]) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment