Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
extension Collection where Self.Iterator.Element == Self.SubSequence.Iterator.Element {
func chunk(size: IndexDistance) -> [[Iterator.Element]] {
let steps = sequence(first: self.startIndex, next: { previous in
self.index(previous, offsetBy: size, limitedBy: self.endIndex)
return{ i -> Array<Iterator.Element> in
let end = self.index(i,
offsetBy: size,
limitedBy: self.endIndex)
?? self.endIndex
return Array(self[i ..< end])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment