Created
April 25, 2015 21:11
-
-
Save neilpa/a93abad78ee1b81c3f20 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
// 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