Skip to content

Instantly share code, notes, and snippets.

@jtodaone
Last active August 22, 2021 09:16
Show Gist options
  • Save jtodaone/59d57ed787aac2ebcdea4da0911dac5a to your computer and use it in GitHub Desktop.
Save jtodaone/59d57ed787aac2ebcdea4da0911dac5a to your computer and use it in GitHub Desktop.
Info: This is now irrelevant since Swift Algorithms package now contains a chunks(ofCount:) method. A simple Swift extension to extend Collection protocol to have a split(into:) function. It is useful when you have to chunk an array or data to equal sized pieces.
// Info: This is now irrelevant since Swift Algorithms package now contains a chunks(ofCount:) method.
extension Collection where Index == Int {
func split(into size: Int) -> [Self.SubSequence] {
return stride(from: 0, to: count, by: size).map {
self[$0 ..< Swift.min($0 + size, count)]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment