Skip to content

Add Array::windows #2068

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Add Array::windows #2068

wants to merge 1 commit into from

Conversation

MINGtoMING
Copy link
Contributor

Add Array::windows.

Copy link

peter-jerry-ye-code-review bot commented May 7, 2025

No input validation for window size parameter

Category
Correctness
Code Snippet
pub fn Array::windows[T](self : Array[T], size : Int) -> Array[ArrayView[T]] {
Recommendation
Add validation to ensure size > 0:

if size <= 0 {
  // handle error case or return empty array
}

Reasoning
The function documentation states that size must be positive, but there's no runtime check. Negative or zero sizes could cause unexpected behavior or panics.

Array capacity could be pre-calculated more efficiently

Category
Performance
Code Snippet
let windows = Array::new(capacity=len)
Recommendation
Calculate capacity inline:

let windows = Array::new(capacity=if self.length() < size { 0 } else { self.length() - size + 1 })```
**Reasoning**
Current implementation calculates length and does bounds checking twice - once for the length check and once for capacity. This could be combined into a single calculation.

</details>
<details>

<summary> Test cases could be more comprehensive </summary>

**Category**
Maintainability
**Code Snippet**
test "array_windows" {
  let arr = [1, 2, 3, 4, 5, 6]
  let windows = arr.windows(11)
**Recommendation**
Add test cases for edge cases:
- Empty array input
- Window size equal to array length
- Window size of 2 (common use case)
- Error cases if input validation is added
**Reasoning**
While current tests cover basic functionality, adding more edge cases would help ensure robustness and serve as documentation for expected behavior in these scenarios.

</details>

@coveralls
Copy link
Collaborator

coveralls commented May 7, 2025

Pull Request Test Coverage Report for Build 6627

Details

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.002%) to 92.364%

Totals Coverage Status
Change from base Build 6625: 0.002%
Covered Lines: 6133
Relevant Lines: 6640

💛 - Coveralls

@FlyCloudC
Copy link
Contributor

How about return an Array[ArrayView[T]]?

@MINGtoMING
Copy link
Contributor Author

MINGtoMING commented May 8, 2025

I agree. Array::windows is typically used in read-only scenarios as well. However, the func interface differs from existing chunks/chunk_by impl, so perhaps they need to be adjusted together.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants