Created
March 30, 2016 14:15
-
-
Save juliengdt/5608f3f7e74a7048d1bd1401c567e6e3 to your computer and use it in GitHub Desktop.
CollectionType Skipper
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
// MARK: - CollectionType Addition | |
extension CollectionType { | |
/** | |
Create and return a new CollectionType made with only skipped items, which index is not a divider of "skip" | |
- parameter skip: the divider index | |
- returns: the new collectionType | |
*/ | |
func skip(skip: Int) -> [Generator.Element] { | |
guard skip != 0 else { return [] } | |
var index = self.startIndex | |
var result: [Generator.Element] = [] | |
var i = 0 | |
repeat { | |
if i % skip == 0 { | |
result.append(self[index]) | |
} | |
index = index.successor() | |
i++ | |
} while (index != self.endIndex) | |
return result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment