asChunks method

(Arr<Arr<T>>, Arr<T>) asChunks(
  1. int chunkSize
)

Splits the slice into a slice of N-element arrays, starting at the beginning of the slice, and a remainder slice with length strictly less than N. Panics if chunkSize is 0 or less.

Implementation

(Arr<Arr<T>> chunks, Arr<T> remainder) asChunks(int chunkSize) {
  if (chunkSize <= 0) {
    panic("'chunkSize' must be positive");
  }
  final length = len();
  final numOfChunks = length ~/ chunkSize;
  final Arr<Arr<T>> chunks = Arr.generate(numOfChunks, (i) {
    return Arr.generate(chunkSize, (j) => getUnchecked(i * chunkSize + j));
  });
  final remainderLength = length % chunkSize;
  var remainder = Arr<T>.generate(
      remainderLength, (i) => getUnchecked(i + chunks.len() * chunkSize));
  return (chunks, remainder);
}