arrays: add arrays.chunk_while/2, where arrays.chunk_while([0,9,2,2,3,2],fn(x int,y int)bool{return x<=y})==[[0,9],[2,2,3],[2]]

This commit is contained in:
Delyan Angelov 2024-07-03 19:01:55 +03:00
parent 8188f65590
commit 879cf1ed21
No known key found for this signature in database
GPG key ID: 66886C0F12D595ED
2 changed files with 47 additions and 0 deletions

View file

@ -147,6 +147,22 @@ fn test_chunk() {
assert chunk[int]([]int{}, 2) == [][]int{}
}
fn test_chunk_while() {
assert chunk_while([0, 9, 2, 2, 3, 2, 7, 5, 9, 5], fn (x int, y int) bool {
return x == y
}) == [[0], [9], [2, 2], [3], [2], [7], [5], [9], [5]]
assert chunk_while([0, 9, 2, 2, 3, 2, 7, 5, 9, 5], fn (x int, y int) bool {
return x <= y
}) == [[0, 9], [2, 2, 3], [2, 7], [5, 9], [5]]
assert chunk_while('aaaabbbcca'.runes(), fn (x rune, y rune) bool {
return x == y
}).map({
it[0]: it.len
}).str() == '[{`a`: 4}, {`b`: 3}, {`c`: 2}, {`a`: 1}]'
}
fn test_window() {
x := [1, 2, 3, 4, 5, 6]