Skip to content

Add Iterable<T>.windows() to apply the sliding window algorithm on Iterables. #724

Open
@vxern

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 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!

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions