Skip to content

Instantly share code, notes, and snippets.

@Nirma
Created February 17, 2016 03:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nirma/25616057dcdde913bee8 to your computer and use it in GitHub Desktop.
Save Nirma/25616057dcdde913bee8 to your computer and use it in GitHub Desktop.
extension Array {
func forEachSlice(sliceSize: Int, block: [Element] -> Void) {
guard sliceSize > 0 && self.count > 0 else {
return
}
var currentSlice = [Element]()
defer {
if !currentSlice.isEmpty {
block(currentSlice)
}
}
for element in self {
currentSlice.append(element)
if currentSlice.count >= sliceSize {
block(currentSlice)
currentSlice.removeAll(keepCapacity: true)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment