Example signature:
extension IterableWindows<T> on Iterable<T> {
Iterable<List<T>> windows(int size) sync* {
...
}
}
Given the following call:
final list = [1, 2, 3, 4, 5];
final size = 2;
print(list.windows(size));
windows() produces a list of slices, where every slice is of size size, and where every consecutive slice begins one element later:
[[1, 2], [2, 3], [3, 4], [4, 5]]
Notes:
- The number of produced windows is equal to
list.length - (size - 1).
- So, given a list of length
4 and window size 2, there would be 3 windows.
Constraints:
size must be a positive, non-zero integer.
size must not exceed the length of list.
If accepted, I'd be happy to implement this myself!