package collect import "sort" func In[T comparable](value T, values ...T) bool { if len(values) == 0 { return false } for _, v := range values { if value == v { return true } } return false } func NotIn[T comparable](value T, values ...T) bool { return !In(value, values...) } func Count[T any](slice []T, isCount func(int, T) bool) int { count := 0 if slice == nil { return count } for i, item := range slice { if isCount(i, item) { count++ } } return count } func Filter[T any](slice []T, predicate func(int, T) bool) []T { var res []T for i, item := range slice { if predicate(i, item) { res = append(res, item) } } return res } // Tail 获取切片尾部元素 // dv: 空切片默认值 func Tail[T any](slice []T, dv T) T { if len(slice) == 0 { return dv } return slice[len(slice)-1] } func MapValues[K comparable, V any](kvs map[K]V) []V { var values []V if len(kvs) == 0 { return values } for _, v := range kvs { values = append(values, v) } return values } func Sum[T int32 | int64 | int](nums []T) T { var sum T = 0 for _, num := range nums { sum += num } return sum } func Max[T int32 | int64 | int](nums []T, dv T) T { if len(nums) == 0 { return dv } var max T = nums[0] for i := 1; i < len(nums); i++ { if nums[i] > max { max = nums[i] } } return max } func Slice2Map[T any, K comparable](slice []T, k func(int, T) K) map[K]T { if slice == nil { return make(map[K]T, 0) } m := make(map[K]T, len(slice)) for i, item := range slice { m[k(i, item)] = item } return m } func Slice2MapKv[T any, K comparable, V any](slice []T, mapping func(int, T) (K, V)) map[K]V { if slice == nil { return make(map[K]V, 0) } m := make(map[K]V, len(slice)) for i, item := range slice { k, v := mapping(i, item) m[k] = v } return m } // SliceEquals compare slice all element equals func SliceEquals[T comparable](slice1 []T, slice2 []T) bool { if len(slice1) != len(slice2) { return false } for i, ele := range slice1 { if ele != slice2[i] { return false } } return true } func Join(slice []string, on string) (res string) { length := len(slice) for i := 0; i < length; i++ { res += slice[i] if i < length-1 { res += on } } return } func CopyMap[K comparable, V any](src map[K]V) (dst map[K]V) { dst = make(map[K]V, len(src)) for k, v := range src { dst[k] = v } return } func CopyMap2[K comparable, K2 comparable, V2 any](src map[K]map[K2]V2) (dst map[K]map[K2]V2) { dst = make(map[K]map[K2]V2, len(src)) for k, v := range src { cv := make(map[K2]V2, len(v)) for k2, v2 := range v { cv[k2] = v2 } dst[k] = cv } return } func IndexMapping[T any](slice []T) (index []int) { index = make([]int, len(slice)) for i := range slice { index[i] = i } return } func SortIndex[T any](slice []T, less func(i, j int) bool) (sortIndex []int) { sortIndex = IndexMapping(slice) sort.Slice(sortIndex, func(i, j int) bool { return less(sortIndex[i], sortIndex[j]) }) return } func SliceMapping[T interface{}, M interface{}](slice []T, mapping func(T) M) (m []M) { if len(slice) == 0 { return } for _, item := range slice { m = append(m, mapping(item)) } return }