feat: 优化 slice 包 Filter 和 Map 函数,新增 Reduce 函数

This commit is contained in:
kercylan98
2023-09-07 20:05:01 +08:00
parent a4ba3f1fa8
commit 5ab990246d
6 changed files with 160 additions and 22 deletions
+28
View File
@@ -0,0 +1,28 @@
package slice
import (
"github.com/kercylan98/minotaur/utils/generic"
)
// Reduce 将切片中的多个元素组合成一个单一值
// - start: 开始索引,如果为负数则从后往前计算,例如:-1 表示从最后一个元素开始向左遍历,1 表示从第二个元素开始
// - slice: 待组合的切片
// - reducer: 组合函数
func Reduce[V any, R generic.Number](start int, slice []V, reducer func(index int, item V, current R) R) (result R) {
length := len(slice)
if start >= length || -start > length {
return
}
if start < 0 {
for i := length + start; i >= 0; i-- {
result = reducer(i, slice[i], result)
}
} else {
for i := start; i < length; i++ {
result = reducer(i, slice[i], result)
}
}
return result
}