windowedTransform<R> method

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

Returns a list of results of applying the given transform function to an each list representing a view over the window of the given size sliding along this collection with the given step.

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<R> windowedTransform<R>(int size, R Function(KtList<T>) transform,
    {int step = 1, bool partialWindows = false}) {
  final list = toList();
  final thisSize = list.size;
  final result = mutableListOf<R>();
  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(transform(window.snapshot()));
    index += step;
  }
  return result;
}