windowed method

Iterable<Iterable<E>> windowed({
  1. required int size,
  2. int step = 1,
  3. bool partialWindows = false,
})

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

Implementation

Iterable<Iterable<E>> windowed({
  required int size,
  int step = 1,
  bool partialWindows = false,
}) {
  if (size <= 0) {
    throw ArgumentError.value(size, 'size', 'must be greater than zero');
  }

  if (step <= 0) {
    throw ArgumentError.value(step, 'step', 'must be greater than zero');
  }

  final length = partialWindows
      ? (this.length / step).ceil()
      : (this.length - size) ~/ step + 1;

  return Iterable.generate(
    length,
    (index) => skip(index * step).take(size),
  );
}