arrays: add chunk and window functions (#11476)

This commit is contained in:
ChAoS_UnItY 2021-09-13 21:13:32 +08:00 committed by GitHub
parent b9dfc89aa9
commit 2ced845e30
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 77 additions and 3 deletions

View file

@ -85,3 +85,24 @@ fn test_group() {
assert z2 == [[8, 2], [9, 1]]
assert group<int>(x, []int{}) == [][]int{}
}
fn test_chunk() {
x := [1, 2, 3, 4, 5]
y := ['a', 'b', 'c', 'd', 'e', 'f']
z1 := chunk<int>(x, 2)
assert z1 == [[1, 2], [3, 4], [5]]
z2 := chunk<string>(y, 3)
assert z2 == [['a', 'b', 'c'], ['d', 'e', 'f']]
assert chunk<int>([]int{}, 2) == [][]int{}
}
fn test_window() {
x := [1, 2, 3, 4, 5, 6]
assert window<int>(x, size: 3) == [[1, 2, 3], [2, 3, 4], [3, 4, 5],
[4, 5, 6],
]
assert window<int>(x, size: 3, step: 2) == [[1, 2, 3], [3, 4, 5]]
assert window<int>([]int{}, size: 2) == [][]int{}
}