windowed method

  1. @useResult
KtList<KtList<T>> windowed(
  1. int size, {
  2. int step = 1,
  3. bool partialWindows = false,
})

Returns a list of snapshots of the window of the given size sliding along this collection with the given step, where each snapshot is a list.

Several last lists may have less elements than the given size.

Both size and step must be positive and can be greater than the number of elements in this collection. @param size the number of elements to take in each window @param step the number of elements to move the window forward by on an each step, by default 1 @param partialWindows controls whether or not to keep partial windows in the end if any, by default false which means partial windows won't be preserved

Implementation

@useResult
KtList<KtList<T>> windowed(int size,
    {int step = 1, bool partialWindows = false}) {
  final list = toList();
  final thisSize = list.size;
  final result = mutableListOf<KtList<T>>();
  final window = _MovingSubList(list);
  var index = 0;
  while (index < thisSize) {
    window.move(index, math.min(thisSize, index + size));
    if (!partialWindows && window.size < size) break;
    result.add(window.snapshot());
    index += step;
  }
  return result;
}