slice

package
v0.0.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 25, 2023 License: Apache-2.0 Imports: 2 Imported by: 110 modules, 131 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add added in v0.0.8

func Add[Src any](src []Src, element Src, index int) ([]Src, error)

Add 在index处添加元素 index 范围应为[0, len(src))

Example
res, _ := Add[int]([]int{1, 2, 3, 4}, 233, 2)
fmt.Println(res)
_, err := Add[int]([]int{1, 2, 3, 4}, 233, -1)
fmt.Println(err)
Output:
[1 2 233 3 4]
ekit: 下标超出范围,长度 4, 下标 -1

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

func ContainsAllFunc[T any](src, dst []T, equal equalFunc[T]) bool

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

func ContainsAnyFunc[T any](src, dst []T, equal equalFunc[T]) bool

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

func ContainsFunc[T any](src []T, equal func(src T) bool) bool

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 Delete

func Delete[Src any](src []Src, index int) ([]Src, error)

Delete 删除 index 处的元素

Example
res, _ := Delete[int]([]int{1, 2, 3, 4}, 2)
fmt.Println(res)
_, err := Delete[int]([]int{1, 2, 3, 4}, -1)
fmt.Println(err)
Output:
[1 2 4]
ekit: 下标超出范围,长度 4, 下标 -1

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

func DiffSetFunc[T any](src, dst []T, equal equalFunc[T]) []T

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 FilterDelete

func FilterDelete[Src any](src []Src, m func(idx int, src Src) bool) []Src

FilterDelete 删除符合条件的元素 考虑到性能问题,所有操作都会在原切片上进行 被删除元素之后的元素会往前移动,有且只会移动一次

func FilterMap

func FilterMap[Src any, Dst any](src []Src, m func(idx int, src Src) (Dst, bool)) []Dst

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 Find added in v0.0.8

func Find[T any](src []T, match matchFunc[T]) (T, bool)

Find 查找元素 如果没有找到,第二个返回值返回 false

Example
val, ok := Find[int]([]int{1, 2, 3}, func(src int) bool {
	return src == 2
})
fmt.Println(val, ok)
val, ok = Find[int]([]int{1, 2, 3}, func(src int) bool {
	return src == 4
})
fmt.Println(val, ok)
Output:
2 true
0 false

func FindAll added in v0.0.8

func FindAll[T any](src []T, match matchFunc[T]) []T

FindAll 查找所有符合条件的元素 永远不会返回 nil

Example
vals := FindAll[int]([]int{2, 3, 4}, func(src int) bool {
	return src%2 == 1
})
fmt.Println(vals)
vals = FindAll[int]([]int{2, 3, 4}, func(src int) bool {
	return src > 5
})
fmt.Println(vals)
Output:
[3]
[]

func Index

func Index[T comparable](src []T, dst T) int

Index 返回和 dst 相等的第一个元素下标 -1 表示没找到

Example
res := Index[int]([]int{1, 2, 3}, 1)
fmt.Println(res)
res = Index[int]([]int{1, 2, 3}, 4)
fmt.Println(res)
Output:
0
-1

func IndexAll

func IndexAll[T comparable](src []T, dst T) []int

IndexAll 返回和 dst 相等的所有元素的下标

Example
res := IndexAll[int]([]int{1, 2, 3, 4, 5, 3, 9}, 3)
fmt.Println(res)
res = IndexAll[int]([]int{1, 2, 3}, 4)
fmt.Println(res)
Output:
[2 5]
[]

func IndexAllFunc

func IndexAllFunc[T any](src []T, match matchFunc[T]) []int

IndexAllFunc 返回和 match 返回 true 的所有元素的下标 你应该优先使用 IndexAll

Example
res := IndexAllFunc[int]([]int{1, 2, 3, 4, 5, 3, 9}, func(src int) bool {
	return src == 3
})
fmt.Println(res)
res = IndexAllFunc[int]([]int{1, 2, 3}, func(src int) bool {
	return src == 4
})
fmt.Println(res)
Output:
[2 5]
[]

func IndexFunc

func IndexFunc[T any](src []T, match matchFunc[T]) int

IndexFunc 返回 match 返回 true 的第一个下标 -1 表示没找到 你应该优先使用 Index

Example
res := IndexFunc[int]([]int{1, 2, 3}, func(src int) bool {
	return src == 1
})
fmt.Println(res)
res = IndexFunc[int]([]int{1, 2, 3}, func(src int) bool {
	return src == 4
})
fmt.Println(res)
Output:
0
-1

func IntersectSet

func IntersectSet[T comparable](src []T, dst []T) []T

IntersectSet 取交集,只支持 comparable 类型 已去重

Example
res := IntersectSet[int]([]int{1, 2, 3, 3, 4}, []int{1, 1, 3})
sort.Ints(res)
fmt.Println(res)
res = IntersectSet[int]([]int{1, 2, 3, 3, 4}, []int{5, 7})
fmt.Println(res)
Output:
[1 3]
[]

func IntersectSetFunc

func IntersectSetFunc[T any](src []T, dst []T, equal equalFunc[T]) []T

IntersectSetFunc 支持任意类型 你应该优先使用 Intersect 已去重

Example
res := IntersectSetFunc[int]([]int{1, 2, 3, 3, 4}, []int{1, 1, 3}, func(src, dst int) bool {
	return src == dst
})
sort.Ints(res)
fmt.Println(res)
res = IntersectSetFunc[int]([]int{1, 2, 3, 3, 4}, []int{5, 7}, func(src, dst int) bool {
	return src == dst
})
fmt.Println(res)
Output:
[1 3]
[]

func LastIndex

func LastIndex[T comparable](src []T, dst T) int

LastIndex 返回和 dst 相等的最后一个元素下标 -1 表示没找到

func LastIndexFunc

func LastIndexFunc[T any](src []T, match matchFunc[T]) int

LastIndexFunc 返回和 dst 相等的最后一个元素下标 -1 表示没找到 你应该优先使用 LastIndex

func Map

func Map[Src any, Dst any](src []Src, m func(idx int, src Src) Dst) []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 Max

func Max[T ekit.RealNumber](ts []T) T

Max 返回最大值。 该方法假设你至少会传入一个值。 在使用 float32 或者 float64 的时候要小心精度问题

Example
res := Max[int]([]int{1, 2, 3})
fmt.Println(res)
Output:
3

func Min

func Min[T ekit.RealNumber](ts []T) T

Min 返回最小值 该方法会假设你至少会传入一个值 在使用 float32 或者 float64 的时候要小心精度问题

Example
res := Min[int]([]int{1, 2, 3})
fmt.Println(res)
Output:
1

func Reverse

func Reverse[T comparable](src []T) []T

Reverse 将会完全创建一个新的切片,而不是直接在 src 上进行翻转。

Example
res := Reverse[int]([]int{1, 3, 2, 2, 4})
fmt.Println(res)
res2 := Reverse[string]([]string{"a", "b", "c", "d", "e"})
fmt.Println(res2)
Output:
[4 2 2 3 1]
[e d c b a]

func ReverseSelf

func ReverseSelf[T comparable](src []T)

ReverseSelf 會直接在 src 上进行翻转。

Example
src := []int{1, 3, 2, 2, 4}
ReverseSelf[int](src)
fmt.Println(src)
src2 := []string{"a", "b", "c", "d", "e"}
ReverseSelf[string](src2)
fmt.Println(src2)
Output:
[4 2 2 3 1]
[e d c b a]

func Sum

func Sum[T ekit.Number](ts []T) T

Sum 求和 在使用 float32 或者 float64 的时候要小心精度问题

Example
res := Sum[int]([]int{1, 2, 3})
fmt.Println(res)
res = Sum[int](nil)
fmt.Println(res)
Output:
6
0

func SymmetricDiffSet

func SymmetricDiffSet[T comparable](src, dst []T) []T

SymmetricDiffSet 对称差集 已去重 返回值的元素顺序是不定的

Example
res := SymmetricDiffSet[int]([]int{1, 3, 4, 2}, []int{2, 5, 7, 3})
sort.Ints(res)
fmt.Println(res)
Output:
[1 4 5 7]

func SymmetricDiffSetFunc

func SymmetricDiffSetFunc[T any](src, dst []T, equal equalFunc[T]) []T

SymmetricDiffSetFunc 对称差集 你应该优先使用 SymmetricDiffSet 已去重

Example
res := SymmetricDiffSetFunc[int]([]int{1, 3, 4, 2}, []int{2, 5, 7, 3}, func(src, dst int) bool {
	return src == dst
})
sort.Ints(res)
fmt.Println(res)
Output:
[1 4 5 7]

func UnionSet

func UnionSet[T comparable](src, dst []T) []T

UnionSet 并集,只支持 comparable 已去重 返回值的元素顺序是不定的

Example
res := UnionSet[int]([]int{1, 3, 4, 5}, []int{1, 4, 7})
sort.Ints(res)
fmt.Println(res)
Output:
[1 3 4 5 7]

func UnionSetFunc

func UnionSetFunc[T any](src, dst []T, equal equalFunc[T]) []T

UnionSetFunc 并集,支持任意类型 你应该优先使用 UnionSet 已去重

Example
res := UnionSetFunc[int]([]int{1, 3, 4, 5}, []int{1, 4, 7}, func(src, dst int) bool {
	return src == dst
})
sort.Ints(res)
fmt.Println(res)
Output:
[1 3 4 5 7]

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL