segmentsIterator<T> function

Iterable<Iterable<T>> segmentsIterator<T>(
  1. List<T> path,
  2. int segmentSize
)

Returns moving window of segments of defined size. With path a,b,c,d,e and segmentSize 3, this method returns: [a,b,c, b,c,d, c,d,e]. If the segments size is bigger then the path length, the method returns the whole path as a one segment.

Implementation

Iterable<Iterable<T>> segmentsIterator<T>(List<T> path, int segmentSize) sync* {
  assert(segmentSize > 0);
  if (segmentSize > path.length) {
    yield path;
    return;
  }
  for (int i = 0; i < path.length - segmentSize + 1; i++) {
    yield path.sublist(i, i + segmentSize);
  }
}