1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-09 23:22:28 +08:00

fix: fix chunk slice bug

This commit is contained in:
dudaodong
2022-11-26 16:21:57 +08:00
parent d36ab5cc3a
commit be148e07ba
2 changed files with 15 additions and 19 deletions

View File

@@ -42,29 +42,18 @@ func Chunk[T any](slice []T, size int) [][]T {
return result
}
length := len(slice)
if size == 1 || size >= length {
for _, v := range slice {
var tmp []T
tmp = append(tmp, v)
result = append(result, tmp)
for _, item := range slice {
l := len(result)
if l == 0 || len(result[l-1]) == size {
result = append(result, []T{})
l++
}
return result
}
// divide slice equally
divideNum := length/size + 1
for i := 0; i < divideNum; i++ {
if i == divideNum-1 {
if len(slice[i*size:]) > 0 {
result = append(result, slice[i*size:])
}
} else {
result = append(result, slice[i*size:(i+1)*size])
}
result[l-1] = append(result[l-1], item)
}
return result
}
// Compact creates an slice with all falsey values removed. The values false, nil, 0, and "" are falsey