vRp.CD2g_test/utils/slice/reduce.go

29 lines
768 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
}