evenlyChunked(in:)

Returns a collection of evenly divided consecutive subsequences of this collection.

Chunked.swift:869
func evenlyChunked(in count: Int) -> EvenlyChunkedCollection<Self>

Parameters

count

The number of chunks to evenly divide this collection into. If this collection is non-empty, count must be greater than zero; otherwise, count may be zero or greater.

Returns

A collection of count subsequences of this collection, divided as evenly as possible.

This method divides the collection into a given number of evenly sized chunks. If the length of the collection is not divisible by count, the chunks at the start will be longer than the chunks at the end, like in this example:

for chunk in "Hello, world!".evenlyChunked(in: 5) {
    print(chunk)
}
// "Hel"
// "lo,"
// " wo"
// "rl"
// "d!"

If the number passed as count is greater than the number of elements in the collection, the result will include one or more empty subsequences.

for chunk in "Hi!".evenlyChunked(in: 5) {
    print(chunk)
}
// "H"
// "i"
// "!"
// ""
// ""