Add Iterable<T>.windows()
to apply the sliding window algorithm on Iterable
s. #724
Open
Description
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 size2
, there would be3
windows.
- So, given a list of length
Constraints:
size
must be a positive, non-zero integer.size
must not exceed the length oflist
.
If accepted, I'd be happy to implement this myself!