chunks(ofCount:)
Returns a collection of subsequences, each with up to the specified length.
func chunks(ofCount count: Int) -> ChunksOfCountCollection<Self>
Parameters
- count
The desired size of each chunk.
Returns
A collection of consescutive, non-overlapping subseqeunces of this collection, where each subsequence (except possibly the last) has the length count
.
If the number of elements in the collection is evenly divided by count
, then every chunk will have a length equal to count
. Otherwise, every chunk but the last will have a length equal to count
, with the remaining elements in the last chunk.
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for chunk in numbers.chunks(ofCount: 5) {
print(chunk)
}
// [1, 2, 3, 4, 5]
// [6, 7, 8, 9, 10]
for chunk in numbers.chunks(ofCount: 3) {
print(chunk)
}
// [1, 2, 3]
// [4, 5, 5]
// [7, 8, 9]
// [10]