Skip to content

Commit df2288b

Browse files
Zhenming-Linpeter-jerry-ye
authored andcommitted
Add Array::windows
1 parent 80b8488 commit df2288b

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

builtin/array.mbt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,6 +1637,45 @@ pub fn[T] Array::chunk_by(
16371637
chunks
16381638
}
16391639
1640+
///|
1641+
/// Generates overlapping subslices (sliding windows) of the specified size.
1642+
///
1643+
/// Parameters:
1644+
///
1645+
/// * `array` : The array to be processed with sliding windows.
1646+
/// * `size` : The window length. Must be a positive integer.
1647+
///
1648+
/// Returns an array of slices, where each inner slice is a contiguous subslice
1649+
/// of the original array. Windows are produced with a step size of 1. If the
1650+
/// original array's length is less than the specified window size, the result
1651+
/// will be an empty array.
1652+
///
1653+
/// Example:
1654+
///
1655+
/// ```moonbit
1656+
/// test "windows" {
1657+
/// let arr = [1, 2, 3, 4, 5]
1658+
/// let windows = arr.windows(2)
1659+
/// inspect!(windows, content="[[1, 2], [2, 3], [3, 4], [4, 5]]")
1660+
/// }
1661+
///
1662+
/// test "empty_windows" {
1663+
/// let arr : Array[Int] = [1, 2]
1664+
/// inspect!(arr.windows(3), content="[]")
1665+
/// }
1666+
/// ```
1667+
pub fn[T] Array::windows(self : Array[T], size : Int) -> Array[ArrayView[T]] {
1668+
let len = self.length() - size + 1
1669+
if len < 1 {
1670+
return []
1671+
}
1672+
let windows = Array::new(capacity=len)
1673+
for i in 0..<len {
1674+
windows.push(self[i:i + size])
1675+
}
1676+
windows
1677+
}
1678+
16401679
///|
16411680
/// Splits an array into chunks using a predicate function. Creates chunks by
16421681
/// grouping consecutive elements that do not satisfy the predicate function.

builtin/array_test.mbt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,17 @@ test "chunks_by" {
498498
)
499499
}
500500

501+
///|
502+
test "array_windows" {
503+
let arr = [1, 2, 3, 4, 5, 6]
504+
let windows = arr.windows(11)
505+
inspect!(windows, content="[]")
506+
let windows = arr.windows(5)
507+
inspect!(windows, content="[[1, 2, 3, 4, 5], [2, 3, 4, 5, 6]]")
508+
let windows = arr.windows(1)
509+
inspect!(windows, content="[[1], [2], [3], [4], [5], [6]]")
510+
}
511+
501512
///|
502513
test "array_get_out_of_bounds" {
503514
let arr = [1, 2, 3]

builtin/builtin.mbti

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ fn[A] Array::unsafe_blit_fixed(Self[A], Int, FixedArray[A], Int, Int) -> Unit
122122
fn[T] Array::unsafe_get(Self[T], Int) -> T
123123
fn[T] Array::unsafe_pop(Self[T]) -> T
124124
fn[A] Array::unsafe_pop_back(Self[A]) -> Unit
125+
fn[T] Array::windows(Self[T], Int) -> Self[ArrayView[T]]
125126
impl[T] Add for Array[T]
126127
impl[T : Compare] Compare for Array[T]
127128
impl[T] Default for Array[T]

0 commit comments

Comments
 (0)