1 Star 0 Fork 0

hongzhaomin / hzm-common-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
lists.go 2.34 KB
一键复制 编辑 原始数据 按行查看 历史
package slice
import (
"errors"
"gitee.com/hongzhaomin/hzm-common-go/strutil"
)
func ContainsIgnoreCase(slice []string, ele string) bool {
for _, s := range slice {
if strutil.EqualsIgnoreCase(s, ele) {
return true
}
}
return false
}
func ContainsIgnoreCaseReturnEle(slice []string, ele string) (bool, string) {
for _, s := range slice {
if strutil.EqualsIgnoreCase(s, ele) {
return true, s
}
}
return false, strutil.Empty
}
func Contains[T comparable](list []T, ele T) bool {
for _, s := range list {
if s == ele {
return true
}
}
return false
}
func ConvertEle[T any, R any](ts []T, t2r func(t T) R) []R {
rs := make([]R, 0, len(ts))
for _, t := range ts {
rs = append(rs, t2r(t))
}
return rs
}
func Distinct[T comparable](list []T) []T {
newList := make([]T, 0, len(list))
for _, t := range list {
if !Contains(newList, t) {
list = append(newList, t)
}
}
return newList
}
func RemoveAt[T comparable](src []T, index int) []T {
if len(src) == 0 {
return src
}
if index < 0 || index > len(src)-1 {
return src
}
return append(src[:index], src[index+1:]...)
}
func Remove[T comparable](src []T, ele T) []T {
index := -1
for i, e := range src {
if ele == e {
index = i
break
}
}
return RemoveAt(src, index)
}
func AddAt[T comparable](src []T, index int, ele T) []T {
if index < 0 || index > len(src) {
return src
}
if index == len(src) {
return append(src, ele)
}
src = append(src, ele)
copy(src[index+1:], src[index:])
src[index] = ele
return src
}
// Page 根据subSize分页
func Page[T comparable](list []T, subSize int) [][]T {
if subSize <= 0 {
panic(errors.New("子切片大小必须大于0"))
}
lists := make([][]T, 0)
BatchConsume(list, subSize, func(eles []T) {
partList := make([]T, 0, len(eles))
partList = append(partList, eles...)
lists = append(lists, partList)
})
return lists
}
// BatchConsume 分批处理,每batchSize一批
func BatchConsume[T comparable](list []T, batchSize int, consumer func([]T)) {
if batchSize <= 0 {
return
}
var (
size = len(list)
flag = true
currentPage, start, end = 1, 0, 0
)
for flag {
start = (currentPage - 1) * batchSize
end = start + batchSize
if end >= size {
end = size
flag = false
} else {
currentPage++
}
// 使用原来的数组
consumer(list[start:end])
}
}
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

搜索帮助