1 Star 0 Fork 0

hongzhaomin / hzm-common-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
number_stream.go 821 Bytes
一键复制 编辑 原始数据 按行查看 历史
hongzhaomin 提交于 2023-06-11 22:29 . 字符串工具类
package streams
import "errors"
type Number interface {
~int | ~int64 | ~float64
}
type NumberStream[N Number] struct {
stream[N]
}
func (ns *NumberStream[N]) Sum() N {
var sum N = 0
for _, i := range ns.list {
sum += i
}
return sum
}
func (ns *NumberStream[N]) Max() N {
if len(ns.list) == 0 {
panic(errors.New("空列表,没有元素"))
}
if len(ns.list) == 1 {
return ns.list[0]
}
max := ns.list[0]
for i := 1; i < len(ns.list); i++ {
cur := ns.list[i]
if max < cur {
max = cur
}
}
return max
}
func (ns *NumberStream[N]) Min() N {
if len(ns.list) == 0 {
panic(errors.New("空列表,没有元素"))
}
if len(ns.list) == 1 {
return ns.list[0]
}
min := ns.list[0]
for i := 1; i < len(ns.list); i++ {
cur := ns.list[i]
if min > cur {
min = cur
}
}
return min
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/hongzhaomin/hzm-common-go.git
git@gitee.com:hongzhaomin/hzm-common-go.git
hongzhaomin
hzm-common-go
hzm-common-go
806eab8bbc74

搜索帮助

344bd9b3 5694891 D2dac590 5694891