Skip to content

Instantly share code, notes, and snippets.

@seanwoodward
Created June 6, 2019 13:51
Show Gist options
  • Save seanwoodward/98d914ffe8919f5d9f57a3e5af87f09d to your computer and use it in GitHub Desktop.
Save seanwoodward/98d914ffe8919f5d9f57a3e5af87f09d to your computer and use it in GitHub Desktop.
Chunk an array into arrays of some size (or smaller)
extension Array {
/// Chunk an array in to an array of arrays of the provided size (or smaller)
/// - Parameters:
/// - size: the size of the chunk
/// - Returns: An array of arrays of `Element`
func chunked(into size: Int) -> [[Element]] {
return Swift.stride(from: 0, to: self.count, by: size).map {
Array(self[$0 ..< Swift.min($0 + size, self.count)])
}
}
}
@seanwoodward
Copy link
Author

use Apple's swift-algorithms, chunked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment