Skip to content

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

Open
bakkot opened this issue Apr 21, 2025 · 4 comments · May be fixed by #14, #15 or #16
Open

behavior of .windows when window size exceeds iterator length #13

bakkot opened this issue Apr 21, 2025 · 4 comments · May be fixed by #14, #15 or #16

Comments

@bakkot
Copy link
Collaborator

bakkot commented Apr 21, 2025

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.

@ljharb
Copy link
Member

ljharb commented Apr 21, 2025

Why is it weird to yield a too-small window as the final result? That's what I'd expect, I think.

@ptomato
Copy link

ptomato commented Apr 21, 2025

I'd expect the return type of .windows(3) to be an iterator yielding zero or more arrays of 3 elements each. Yielding an undersized window means you have to check for undersized windows before you can address elements in the windows, or always remember to deal with the elements being undefined. Programmers will most likely forget this.

@ljharb
Copy link
Member

ljharb commented Apr 21, 2025

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.

@acutmore
Copy link

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 0 instead of undefined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment