windowed method

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

Returns sliding windows of size.

Implementation

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

  final source = toList();
  final output = <List<T>>[];
  for (var i = 0; i < source.length; i += step) {
    final end = i + size;
    if (end <= source.length) {
      output.add(source.sublist(i, end));
    } else if (partialWindows && i < source.length) {
      output.add(source.sublist(i));
    }
  }
  return output;
}