File: rolling.go

package info (click to toggle)
golang-github-gigawattio-window 0.0~git20180317.0f5467e-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, bullseye-backports, forky, sid, trixie
  • size: 80 kB
  • sloc: makefile: 2
file content (24 lines) | stat: -rw-r--r-- 482 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package window

// Rolling generates a rolling window of size N for a sequence of string tokens.
func Rolling(elements []string, n int) [][]string {
	if len(elements) == 0 || len(elements) < n || n <= 0 {
		return nil
	}

	var (
		accum = make([][]string, len(elements)+1-n)
		j     int
	)

	for i := 0; i < len(elements)+1-n; i++ {
		win := make([]string, n)
		win[0] = elements[i]
		for j = 0; j+1 < n; j++ {
			win[j+1] = elements[i+j+1]
		}
		accum[i] = win
	}

	return accum
}