Created
June 6, 2019 13:51
-
-
Save seanwoodward/98d914ffe8919f5d9f57a3e5af87f09d to your computer and use it in GitHub Desktop.
Chunk an array into arrays of some size (or smaller)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)]) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
use Apple's swift-algorithms, chunked