@@ -1639,6 +1639,47 @@ pub fn Array::chunk_by[T](
16391639 chunks
16401640}
16411641
1642+ ///|
1643+ /// Generates consecutive overlapping subarrays of the specified window size.
1644+ ///
1645+ /// Parameters:
1646+ ///
1647+ /// * `array` : The array to be processed with sliding windows.
1648+ /// * `size` : The window length. Must be a positive integer.
1649+ ///
1650+ /// Returns an array of subarrays, where each subarray contains size consecutive elements
1651+ /// starting from successive positions. Consecutive windows overlap by all but one element.
1652+ /// If the input array's length is less than the window size, no valid windows are produced
1653+ /// and the result is an empty array.
1654+ ///
1655+ /// Example:
1656+ ///
1657+ /// ```moonbit
1658+ /// test "windows" {
1659+ /// let arr = [1, 2, 3, 4, 5]
1660+ /// let windows = arr.windows(2)
1661+ /// inspect!(windows, content="[[1, 2], [2, 3], [3, 4], [4, 5]]")
1662+ /// }
1663+ ///
1664+ /// test "empty_windows" {
1665+ /// let arr : Array[Int] = [1, 2]
1666+ /// inspect!(arr.windows(3), content="[]")
1667+ /// }
1668+ /// ```
1669+ pub fn Array ::windows[T ](self : Array [T ], size : Int ) -> Array [Array [T ]] {
1670+ let capacity = self.length() - size + 1
1671+ if capacity < 1 {
1672+ return []
1673+ }
1674+ let windows = Array ::new(capacity~)
1675+ for i in 0..<capacity {
1676+ let window = Array ::make_uninit(size)
1677+ Array ::unsafe_blit(window, 0, self, i, size)
1678+ windows.push(window)
1679+ }
1680+ windows
1681+ }
1682+
16421683///|
16431684/// Splits an array into chunks using a predicate function. Creates chunks by
16441685/// grouping consecutive elements that do not satisfy the predicate function.
0 commit comments