-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsliding.go
More file actions
23 lines (19 loc) · 646 Bytes
/
sliding.go
File metadata and controls
23 lines (19 loc) · 646 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package underscore
// Sliding creates a sliding window view of the slice with the specified window size.
// Returns an empty slice if size is less than or equal to 0.
// Returns an empty slice if size is greater than the slice length.
//
// Example: Sliding([]int{1,2,3,4,5}, 3) → [[1,2,3], [2,3,4], [3,4,5]]
func Sliding[T any](values []T, size int) [][]T {
if size <= 0 || size > len(values) {
return [][]T{}
}
windowCount := len(values) - size + 1
res := make([][]T, 0, windowCount)
for i := 0; i <= len(values)-size; i++ {
window := make([]T, size)
copy(window, values[i:i+size])
res = append(res, window)
}
return res
}