slice

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2023 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

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

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, dst T, equal equalFunc[T]) bool

ContainsFunc 判断 src 里面是否存在 dst 你应该优先使用 Contains

Example
res := ContainsFunc[int]([]int{1, 2, 3}, 3, func(src, dst int) bool {
	return src == dst
})
fmt.Println(res)
Output:

true

func Delete added in v0.0.4

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 FilterMap added in v0.0.4

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 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, dst T, equal equalFunc[T]) []int

IndexAllFunc 返回和 dst 相等的所有元素的下标 你应该优先使用 IndexAll

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

[2 5]
[]

func IndexFunc

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

IndexFunc 返回和 dst 相等的第一个元素下标 -1 表示没找到 你应该优先使用 Index

Example
res := IndexFunc[int]([]int{1, 2, 3}, 1, func(src, dst int) bool {
	return src == dst
})
fmt.Println(res)
res = IndexFunc[int]([]int{1, 2, 3}, 4, func(src, dst int) bool {
	return src == dst
})
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, dst T, equal equalFunc[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 added in v0.0.4

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

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