windowed method

Iterable<List<T>> windowed(
  1. int size, {
  2. int step = 1,
  3. bool partial = false,
})

Returns a sliding window of elements of size size.

step defines the window shift (default 1). If partial is true, smaller windows at the end are included.

Implementation

Iterable<List<T>> windowed(
  int size, {
  int step = 1,
  bool partial = false,
}) sync* {
  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 list = toList();
  for (var i = 0; i < list.length; i += step) {
    final end = i + size;
    if (end <= list.length) {
      yield list.sublist(i, end);
    } else if (partial && i < list.length) {
      yield list.sublist(i);
    }
  }
}