Documentation
¶
Overview ¶
Package sliceutil 提供对切片的相关功能
Index ¶
- func AnySlice[S ~[]T, T any](slices S) []any
- func At[S ~[]T, T any](slice S, eq func(T, int) bool) (T, bool)
- func Contains[S ~[]T, T any](container, sub S, eq func(i, j T) bool) bool
- func Count[S ~[]T, T any](slice S, eq func(T, int) bool) (count int)
- func Delete[S ~[]T, T any](slice S, eq func(T, int) bool) S
- func Dup[S ~[]T, T any](slice S, eq func(i, j T) bool) (indexes []int)
- func Exists[S ~[]T, T any](slice S, eq func(T, int) bool) bool
- func Filter[S ~[]T, T any](slices S, f func(T, int) bool) []T
- func Index[S ~[]T, T any](slice S, eq func(T, int) bool) (index int)
- func Indexes[S ~[]T, T any](slice S, eq func(T, int) bool) (indexes []int)
- func MapKeys[M ~map[K]V, K comparable, V any](m M) []K
- func MapVals[M ~map[K]V, K comparable, V any](m M) []V
- func Max[S ~[]T, T any](slices S, less func(i, j T) bool) T
- func Min[S ~[]T, T any](slices S, less func(i, j T) bool) T
- func QuickDelete[S ~[]T, T any](slice S, eq func(T, int) bool) S
- func Reverse[S ~[]T, T any](slice S)deprecated
- func SafeFilter[S ~[]T, T any](slices S, f func(T, int) bool) []T
- func Unique[S ~[]T, T any](slice S, eq func(i, j T) bool) S
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func At ¶ added in v0.9.0
At 从 slice 中查找符合 eq 的元素
Example ¶
intSlice := []int{1, 2, 3, 7, 0, 4, 7} v, found := At(intSlice, func(e, _ int) bool { return e == 7 }) fmt.Println(found, v)
Output: true 7
func Contains ¶ added in v0.4.0
Contains container 是否包含了 sub 中的所有元素
container 与 sub 都必须是数组或是切片类型。 如果只是需要判断某一个值是否在 container 中,可以使用 Count() 函数。 eq 用于判断两个数组或是切的某个元素是否相等,其原型为:
func(i, j int) bool
i 表示 sub 的第 i 个元素,j 表示 container 的第 j 个元素,两者顺序不能乱。
Example ¶
ints1 := []int{1, 2, 3, 4, 5} ints2 := []int{1, 5, 2} fmt.Println(Contains(ints1, ints2, func(i, j int) bool { return i == j }))
Output: true
func Count ¶
Count 检测数组中指定值的数量
Example ¶
intSlice := []int{1, 2, 3, 7, 0, 4, 7} fmt.Println(Count(intSlice, func(e, _ int) bool { return e == 7 }))
Output: 2
func Delete ¶ added in v0.2.0
Delete 删除 slice 中符合 eq 条件的元素
eq 对比函数,用于确定指定的元素是否可以删除,返回 true 表示可以删除;
Example ¶
intSlice := []int{1, 2, 3, 7, 0, 4, 7} rslt := Delete(intSlice, func(e, _ int) bool { return e == 7 }) fmt.Println("Delete:", rslt) intSlice = []int{1, 2, 3, 7, 0, 4, 7} rslt = QuickDelete(intSlice, func(e, _ int) bool { return e == 7 || e == 2 }) fmt.Println("QuickDelete:", rslt)
Output: Delete: [1 2 3 0 4] QuickDelete: [1 4 3 0]
func Dup ¶
Dup 检测数组或是切片中是否包含重复的值
在存在相同元素时,会返回该相同元素的下标列表, 当有多组相同元素时,仅返回第一组相同元素的下标。
Example ¶
intSlice := []int{1, 2, 3, 7, 0, 4, 7} fmt.Println(Dup(intSlice, func(i, j int) bool { return i == j }))
Output: [3 6]
func Exists ¶ added in v0.10.0
Exists 判断 slice 中是否存在符合 eq 的元素存在
Example ¶
intSlice := []int{1, 2, 3, 7, 0, 4, 7} fmt.Println(Exists(intSlice, func(e, _ int) bool { return e == 7 }))
Output: true
func Index ¶ added in v0.7.0
Index 从 slice 查找符合 eq 的第一个元素并返回其在数组中的元素
Example ¶
intSlice := []int{1, 2, 3, 7, 0, 4, 7} fmt.Println(Index(intSlice, func(e, _ int) bool { return e == 7 }))
Output: 3
func MapKeys ¶ added in v0.16.0
func MapKeys[M ~map[K]V, K comparable, V any](m M) []K
MapKeys 获取一个 map 的所有 key
func MapVals ¶ added in v0.16.0
func MapVals[M ~map[K]V, K comparable, V any](m M) []V
MapVals 获取一个 map 的所有值
func QuickDelete ¶ added in v0.2.0
QuickDelete 删除 slice 中符合 eq 条件的元素
功能与 Delete 相同,但是性能相对 Delete 会好一些,同时也不再保证元素顺序与原数组相同。
func Reverse
deprecated
added in
v0.3.0
func SafeFilter ¶ added in v0.13.0
SafeFilter 过滤数据
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.