-
Notifications
You must be signed in to change notification settings - Fork 1
behavior of .windows
when window size exceeds iterator length
#13
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
Comments
Why is it weird to yield a too-small window as the final result? That's what I'd expect, I think. |
I'd expect the return type of |
Which is more likely, though - a mistake caused by not noticing that your iterator doesn't yield complete chunks, or, one caused by not noticing that a partial chunk was dropped? This probably calls for an option, if the dropping behavior is needed. |
I think the default should be that if you ask for a window of 5 items, you need 5 items to get a window. In Kotlin you have to opt in to "partialWindows": https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/windowed.html val partialWindows = sequence.take(10).windowed(size = 5, step = 3, partialWindows = true)
println(partialWindows.toList()) // [[1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [7, 8, 9, 10], [10]] Having an options bag to choose a mode would align with https://github.com/tc39/proposal-joint-iteration, which currently defaults to "shortest", dropping values that can't be zipped with anything. With windows having an option to choose the padding may also be useful, i.e. choosing |
After revisiting this in the TG3 call, I realized that my intuition (that smaller groups are ideal/expected) applies to chunks, not to windows. With "windows", which i find a bit of a confusing name and a bit of an undermotivated use case, I would indeed expect values that don't fit in a window to be dropped, such that you only get windows that match the desired length. |
Also following the TG3 call, my expectation is that each window must have the specified number of values. But, I also expect to be able to observe and react to the partial-window that was accumulated if the receiver failed to produce enough elements to fill a window. If there are multiple consumers, I expect only one of those consumers to see the partial-window, if for example they need to dispose of the corresponding resource exactly once, as is consistent with the return value of a generator function. To that end, please consider using the const small = Iterator.from([1, 2, 3, 4, 5]).windows(3);
small.next(); // { done: false, value: [1, 2, 3] }
small.next(); // { done: true, value: [4, 5] }
small.next(); // { done: true, value: undefined }
small.next(); // { done: true, value: undefined } const empty = Iterator.from([]).windows(3);
empty.next(); // { done: true, value: undefined } const single = Iterator.from([1, 2, 3]).windows(3);
single.next(); // { done: false, value: [1, 2, 3] }
single.next(); // { done: true, value: undefined } We already have sufficient affordances, in my opinion, to react to the final/return value of an iterator, such as const partial = for (const window of sequence().windows(3)) {
console.log(Math.sum(...window) / 3);
};
if (partial) {
console.log(Math.sum(...partial) / partial.length);
} |
For reference, this non-undefined value on the first |
Uh oh!
There was an error while loading. Please reload this page.
What happens if you do
[1, 2].values().windows(3)
?Current spec says this gives you an empty iterator (i.e., no windows at all). As the readme documents, behavior in other languages varies.
My intuition is that it's weird to drop items, but it's also weird to yield a too-small window. I'm not sure what the right behavior is. Throwing is a possibility.
The text was updated successfully, but these errors were encountered: