asRchunks method

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

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

Implementation

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