Documentation
¶
Overview ¶
Package sliceX 提供切片操作的工具函数,包括过滤、映射、去重、集合运算等。
Index ¶
- func Contains[T comparable](src []T, dst T) bool
- func ContainsAll[T comparable](src, dst []T) bool
- func ContainsAllFunc[T any](src, dst []T, equal EqualFunc[T]) bool
- func ContainsAny[T comparable](src, dst []T) bool
- func ContainsAnyFunc[T any](src, dst []T, equal EqualFunc[T]) bool
- func ContainsFunc[T any](src []T, equal func(src T) bool) bool
- func DiffSet[T comparable](src, dst []T) []T
- func DiffSetFunc[T any](src, dst []T, equal EqualFunc[T]) []T
- func FilterMap[Src any, Dst any](src []Src, m func(idx int, src Src) (Dst, bool)) []Dst
- func Map[Src any, Dst any](src []Src, m func(idx int, src Src) Dst) []Dst
- func MapKeyFind(data map[string]any, path string, isRexp bool) ([]any, bool)
- func Max[T NumberOrString](a []T) T
- func MaxSafe[T NumberOrString](a []T) (T, bool)
- func Min[T NumberOrString](a []T) T
- func MinSafe[T NumberOrString](a []T) (T, bool)
- func ReverseSlice[T NumberOrString](s []T) []T
- func SortAsc[T NumberOrString](s []T) []T
- func SortDesc[T NumberOrString](s []T) []T
- func Sum[T NumberOrString](s []T) T
- func ToMap[Ele any, Key comparable](elements []Ele, fn func(element Ele) Key) map[Key]Ele
- func ToMapV[Ele any, Key comparable, Val any](elements []Ele, fn func(element Ele) (Key, Val)) (resultMap map[Key]Val)
- type EqualFunc
- type NumberOrString
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contains ¶
func Contains[T comparable](src []T, dst T) bool
Contains 判断 src 里面是否存在 dst
Example ¶
res := Contains[int]([]int{1, 2, 3}, 3)
fmt.Println(res)
Output: true
func ContainsAll ¶
func ContainsAll[T comparable](src, dst []T) bool
ContainsAll 判断 src 里面是否存在 dst 中的所有元素
Example ¶
res := ContainsAll[int]([]int{1, 2, 3}, []int{3, 1})
fmt.Println(res)
res = ContainsAll[int]([]int{1, 2, 3}, []int{3, 1, 4})
fmt.Println(res)
Output: true false
func ContainsAllFunc ¶
ContainsAllFunc 判断 src 里面是否存在 dst 中的所有元素 你应该优先使用 ContainsAll
Example ¶
res := ContainsAllFunc[int]([]int{1, 2, 3}, []int{3, 1}, func(src, dst int) bool {
return src == dst
})
fmt.Println(res)
res = ContainsAllFunc[int]([]int{1, 2, 3}, []int{3, 1, 4}, func(src, dst int) bool {
return src == dst
})
fmt.Println(res)
Output: true false
func ContainsAny ¶
func ContainsAny[T comparable](src, dst []T) bool
ContainsAny 判断 src 里面是否存在 dst 中的任何一个元素
Example ¶
res := ContainsAny[int]([]int{1, 2, 3}, []int{3, 6})
fmt.Println(res)
res = ContainsAny[int]([]int{1, 2, 3}, []int{4, 5, 9})
fmt.Println(res)
Output: true false
func ContainsAnyFunc ¶
ContainsAnyFunc 判断 src 里面是否存在 dst 中的任何一个元素 你应该优先使用 ContainsAny
Example ¶
res := ContainsAnyFunc[int]([]int{1, 2, 3}, []int{3, 1}, func(src, dst int) bool {
return src == dst
})
fmt.Println(res)
res = ContainsAllFunc[int]([]int{1, 2, 3}, []int{4, 7, 6}, func(src, dst int) bool {
return src == dst
})
fmt.Println(res)
Output: true false
func ContainsFunc ¶
ContainsFunc 判断 src 里面是否存在 dst 你应该优先使用 Contains
Example ¶
res := ContainsFunc[int]([]int{1, 2, 3}, func(src int) bool {
return src == 3
})
fmt.Println(res)
Output: true
func DiffSet ¶
func DiffSet[T comparable](src, dst []T) []T
DiffSet 差集,只支持 comparable 类型 已去重 并且返回值的顺序是不确定的
Example ¶
res := DiffSet[int]([]int{1, 3, 2, 2, 4}, []int{3, 4, 5, 6})
sort.Ints(res)
fmt.Println(res)
Output: [1 2]
func DiffSetFunc ¶
DiffSetFunc 差集,已去重 你应该优先使用 DiffSet
Example ¶
res := DiffSetFunc[int]([]int{1, 3, 2, 2, 4}, []int{3, 4, 5, 6}, func(src, dst int) bool {
return src == dst
})
fmt.Println(res)
Output: [1 2]
func FilterMap ¶
FilterMap 执行过滤并且转化 如果 m 的第二个返回值是 false,那么我们会忽略第一个返回值 即便第二个返回值是 false,后续的元素依旧会被遍历
Example ¶
src := []int{1, -2, 3}
dst := FilterMap[int, string](src, func(idx int, src int) (string, bool) {
return strconv.Itoa(src), src >= 0
})
fmt.Println(dst)
Output: [1 3]
func Map ¶
Map 将一个 []Src 类型的切片,通过一个映射函数 m,转换为一个新的 []Dst 类型的切片。
Example ¶
src := []int{1, 2, 3}
dst := Map(src, func(idx int, src int) string {
return strconv.Itoa(src)
})
fmt.Println(dst)
Output: [1 2 3]
func MapKeyFind ¶
MapKeyFind 支持:【注意rexp参数是如果key本身就有【. eg: "user.name"是一个key】,而非作为正则,true为正则,false为非正则】
- 【10层以下map,直接false即可,和精确性能相似】
- 【警告】模糊匹配(isRexp=false)会遍历整个结构,时间复杂度高, 不建议在 >100 层或 >1000 节点的结构中使用。
- 精确: user.name 【1速度,最优】
- 通配展开: users.*.name 【2速度,仅次精确】
- key 模糊: users.*name2, users.name*【2速度,仅次精确】
- 模糊匹配: name (任意层级) 【3速度,仅用于小数据,大数据影响性能,eg: map嵌套100甚至1000层map】
func ReverseSlice ¶
func ReverseSlice[T NumberOrString](s []T) []T
ReverseSlice 对传入的切片进行逆序,返回一个新的切片,不修改原切片
func ToMap ¶
func ToMap[Ele any, Key comparable]( elements []Ele, fn func(element Ele) Key, ) map[Key]Ele
ToMap 将[]Ele映射到map[Key]Ele 从Ele中提取Key的函数fn由使用者提供
注意: 如果出现 i < j 设:
key_i := fn(elements[i]) key_j := fn(elements[j])
满足key_i == key_j 的情况,则在返回结果的resultMap中 resultMap[key_i] = val_j
即使传入的字符串为nil,也保证返回的map是一个空map而不是nil
Example ¶
elements := []string{"1", "2", "3", "4", "5"}
resMap := ToMap(elements, func(str string) int {
num, _ := strconv.Atoi(str)
return num
})
fmt.Println(resMap)
Output: map[1:1 2:2 3:3 4:4 5:5]
func ToMapV ¶
func ToMapV[Ele any, Key comparable, Val any]( elements []Ele, fn func(element Ele) (Key, Val), ) (resultMap map[Key]Val)
ToMapV 将[]Ele映射到map[Key]Val 从Ele中提取Key和Val的函数fn由使用者提供
注意: 如果出现 i < j 设:
key_i, val_i := fn(elements[i]) key_j, val_j := fn(elements[j])
满足key_i == key_j 的情况,则在返回结果的resultMap中 resultMap[key_i] = val_j
即使传入的字符串为nil,也保证返回的map是一个空map而不是nil
Example ¶
type eleType struct {
A string
B string
C int
}
type eleTypeOut struct {
A string
B string
}
elements := []eleType{
{
A: "a",
B: "b",
C: 1,
},
{
A: "c",
B: "d",
C: 2,
},
}
resMap := ToMapV(elements, func(ele eleType) (string, eleTypeOut) {
return ele.A, eleTypeOut{
A: ele.A,
B: ele.B,
}
})
fmt.Println(resMap)
Output: map[a:{a b} c:{c d}]