Skip to content

Instantly share code, notes, and snippets.

@juliengdt
Created March 30, 2016 14:15
Show Gist options
  • Save juliengdt/5608f3f7e74a7048d1bd1401c567e6e3 to your computer and use it in GitHub Desktop.
Save juliengdt/5608f3f7e74a7048d1bd1401c567e6e3 to your computer and use it in GitHub Desktop.
CollectionType Skipper
// 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