Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(collections): support Iterable argument in slidingWindows() #5916

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

Liam-Tait
Copy link
Contributor

@Liam-Tait Liam-Tait commented Sep 5, 2024

Support Iterable for collections slidingWindows

Maintains the current sliding windows for Arrays for the empty Array case. Using the iterable version would change the output from an empty array causing the case is covered by the "slidingWindows() handles empty Array" test to fail.

Example:

slidingWindows(new Array(3), 1)

// The difference is 
// [ [ <1 empty item> ], [ <1 empty item> ], [ <1 empty item> ] ]
// vs
// [ [ undefined ], [ undefined ], [ undefined ] ]

If the array specific implementation was removed, this would become a breaking change

With an infinite iterator this function would never return.
I added a maxWindows option that limits the number of windows created which would be useful to prevent that case. (maybe it should throw instead?)

However I can remove if we think a seperate take function is better
or the onus is on the user
e.g

function* take<T>(iterable: Iterable<T>, count: number): Iterable<T> {
  let i = 0;
  for (const item of iterable) {
    if (i++ >= count) {
      break;
    }
    yield item;
  }
}
function* infiniteNumbers() {
  let i = 0;
  while (true) {
    yield i++;
  }
}

slidingWindows(
  take(infiniteNumbers(), 5),
  3,
); // [ [ 0, 1, 2 ], [ 1, 2, 3 ], [ 2, 3, 4 ] ]

#5470

Copy link

codecov bot commented Sep 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.26%. Comparing base (b3e1ebb) to head (d40d2a6).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5916   +/-   ##
=======================================
  Coverage   96.25%   96.26%           
=======================================
  Files         489      489           
  Lines       39457    39499   +42     
  Branches     5824     5839   +15     
=======================================
+ Hits        37980    38022   +42     
  Misses       1433     1433           
  Partials       44       44           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Liam-Tait Liam-Tait marked this pull request as ready for review September 5, 2024 10:05
@Liam-Tait Liam-Tait requested a review from kt3k as a code owner September 5, 2024 10:05
@Liam-Tait Liam-Tait changed the title feat(collections): support Iterableinput in slidingWindows() feat(collections): support Iterable argument in slidingWindows() Sep 5, 2024
@Liam-Tait Liam-Tait marked this pull request as draft September 5, 2024 10:14
@Liam-Tait Liam-Tait marked this pull request as ready for review September 9, 2024 09:04
@Liam-Tait
Copy link
Contributor Author

Liam-Tait commented Sep 9, 2024

On second thought, maxWindows is not really needed.

Added comment to the tracking issue to discuss

@Liam-Tait Liam-Tait marked this pull request as draft September 9, 2024 10:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant