Documentation ¶
Index ¶
- func Chunk[T any](collection []T, size int) [][]T
- func Combinations[T any](a []T) [][]T
- func CombinationsPari[T any](a, b []T, aCount, bCount int) [][]T
- func Contains[V comparable](slice []V, value V) bool
- func ContainsAny[V any](slice []V, values V) bool
- func Copy[V any](slice []V) []V
- func CopyMatrix[V any](slice [][]V) [][]V
- func Del[V any](slice *[]V, index int)
- func Distinct[V any](slice []V) []V
- func Drop[V any](start, n int, slice []V) []V
- func DropBy[V any](slice []V, fn func(index int, value V) bool) []V
- func Each[V any](abort bool, slice []V, iterator func(index int, item V) bool)
- func EachF[V any](slice []V, iterator func(index int, item V) bool)
- func EachResult[V any, R any](abort bool, slice []V, iterator func(index int, item V) (R, bool)) []R
- func EachResultF[V any, R any](slice []V, iterator func(index int, item V) (R, bool)) []R
- func EachResultReverse[V any, R any](abort bool, slice []V, iterator func(index int, item V) (R, bool)) []R
- func EachResultReverseF[V any, R any](slice []V, iterator func(index int, item V) (R, bool)) []R
- func EachResultReverseT[V any, R any](slice []V, iterator func(index int, item V) (R, bool)) []R
- func EachResultT[V any, R any](slice []V, iterator func(index int, item V) (R, bool)) []R
- func EachReverse[V any](abort bool, slice []V, iterator func(index int, item V) bool)
- func EachReverseF[V any](slice []V, iterator func(index int, item V) bool)
- func EachReverseT[V any](slice []V, iterator func(index int, item V) bool)
- func EachT[V any](slice []V, iterator func(index int, item V) bool)
- func FillBy[V any](slice []V, fn func(index int, value V) V) []V
- func FillByCopy[V any](slice []V, fn func(index int, value V) V) []V
- func FillUntil[V any](abort bool, slice []V, fn func(index int, value V) (V, bool)) []V
- func FillUntilCopy[V any](abort bool, slice []V, fn func(index int, value V) (V, bool)) []V
- func FillUntilF[V any](slice []V, fn func(index int, value V) (V, bool)) []V
- func FillUntilFCopy[V any](slice []V, fn func(index int, value V) (V, bool)) []V
- func FillUntilT[V any](slice []V, fn func(index int, value V) (V, bool)) []V
- func FillUntilTCopy[V any](slice []V, fn func(index int, value V) (V, bool)) []V
- func Filter[V any](reserve bool, slice []V, expression func(index int, item V) bool) []V
- func FilterCopy[V any](reserve bool, slice []V, expression func(index int, item V) bool) []V
- func FilterF[V any](slice []V, expression func(index int, item V) bool) []V
- func FilterFCopy[V any](slice []V, expression func(index int, item V) bool) []V
- func FilterT[V any](slice []V, expression func(index int, item V) bool) []V
- func FilterTCopy[V any](slice []V, expression func(index int, item V) bool) []V
- func GetEndPart[V any](slice []V, n int) []V
- func GetIndex[V comparable](slice []V, value V) int
- func GetIndexAny[V any](slice []V, values V) int
- func GetPart[V any](slice []V, start, end int) []V
- func GetStartPart[V any](slice []V, n int) []V
- func GetValue[V any](slice []V, i int) (v V)
- func GetValueHandle[V any](slice []V, i int, handle func(v V))
- func GroupBy[T any, K comparable](collection []T, fn func(T) K) map[K][]T
- func Insert[V any](slice *[]V, index int, value V)
- func IsIntersect[T any](a, b []T) bool
- func IsIntersectWithCheck[T any](a, b []T, checkHandle func(a, b T) bool) bool
- func LimitedCombinations[T any](a []T, minSize, maxSize int) [][]T
- func Map[V any, T any](slice []V, converter func(index int, item V) T) []T
- func Merge[V any](slices ...[]V) []V
- func Move[V any](slice *[]V, index, to int)
- func NextLoop[V any](slice []V, i int) (next int, value V)
- func PrevLoop[V any](slice []V, i int) (prev int, value V)
- func Reduce[V any, R generic.Number](start int, slice []V, reducer func(index int, item V, current R) R) (result R)
- func Reverse[V any](slice []V)
- func Shuffle[T any](collection []T) []T
- func ShuffleCopy[T any](collection []T) []T
- func SubWithCheck[T any](a, b []T, checkHandle func(a, b T) bool) []T
- func Swap[V any](slice []V, i, j int)
- func ToMap[K comparable, V any](slice []V, key func(V) K) map[K]V
- func ToSet[V comparable](slice []V) map[V]struct{}
- func Unique[T comparable](collection []T) []T
- func UniqueBy[T any](collection []T, fn func(T) any) []T
- func Zoom[V any](newSize int, slice []V) []V
- type Priority
- func (slf *Priority[V]) Action(action func(items []*PriorityItem[V]) []*PriorityItem[V])
- func (slf *Priority[V]) Append(v V, priority int)
- func (slf *Priority[V]) Appends(priority int, vs ...V)
- func (slf *Priority[V]) Cap() int
- func (slf *Priority[V]) Clear()
- func (slf *Priority[V]) Get(index int) *PriorityItem[V]
- func (slf *Priority[V]) GetPriority(index int) int
- func (slf *Priority[V]) GetValue(index int) V
- func (slf *Priority[V]) Len() int
- func (slf *Priority[V]) Range(action func(index int, item *PriorityItem[V]) bool)
- func (slf *Priority[V]) RangePriority(action func(index int, priority int) bool)
- func (slf *Priority[V]) RangeValue(action func(index int, value V) bool)
- func (slf *Priority[V]) Set(index int, value V, priority int)
- func (slf *Priority[V]) SetPriority(index int, priority int)
- func (slf *Priority[V]) SetValue(index int, value V)
- func (slf *Priority[V]) Slice() []V
- func (slf *Priority[V]) String() string
- type PriorityItem
- type Slice
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Chunk ¶ added in v0.1.5
Chunk 返回分块后的切片
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/slice" ) func main() { var collection = []int{1, 2, 3, 4, 5, 6, 7, 8, 9} var chunks = slice.Chunk(collection, 3) for _, chunk := range chunks { fmt.Println(chunk) } }
Output: [1 2 3] [4 5 6] [7 8 9]
func Combinations ¶ added in v0.0.20
func Combinations[T any](a []T) [][]T
Combinations 获取给定数组的所有组合,包括重复元素的组合
func CombinationsPari ¶ added in v0.0.21
CombinationsPari 从给定的两个数组中按照特定数量得到所有组合后,再将两个数组的组合进行组合
- 例如从数组 A 中得到所有数量为2的组合,从数组 B 中得到所有数量为3的组合,再将两个组合进行组合,得到所有数量为5的组合
func Contains ¶ added in v0.0.15
func Contains[V comparable](slice []V, value V) bool
Contains 判断数组是否包含某个元素
func ContainsAny ¶ added in v0.0.15
ContainsAny 判断数组是否包含某个元素
func Drop ¶ added in v0.1.5
Drop 从切片中删除指定数量的元素
- start: 起始位置
- n: 删除的元素数量
- slice: 待删除元素的切片
关于 start 的取值:
- 当 start < 0 时,start 将会从右至左计数,即 -1 表示最后一个元素,-2 表示倒数第二个元素,以此类推
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/slice" ) func main() { fmt.Println(slice.Drop(1, 3, []int{1, 2, 3, 4, 5})) }
Output: [1 5]
func EachResult ¶ added in v0.1.5
func EachResult[V any, R any](abort bool, slice []V, iterator func(index int, item V) (R, bool)) []R
EachResult 根据传入的 abort 遍历 slice,得到遍历的结果,如果 iterator 返回值中的 bool 值与 abort 相同,则停止遍历,并返回当前已积累的结果
func EachResultF ¶ added in v0.1.5
EachResultF 与 EachResult 的功能相同,但是 abort 被默认为 false
func EachResultReverse ¶ added in v0.1.5
func EachResultReverse[V any, R any](abort bool, slice []V, iterator func(index int, item V) (R, bool)) []R
EachResultReverse 根据传入的 abort 从后往前遍历 slice,得到遍历的结果,如果 iterator 返回值中的 bool 值与 abort 相同,则停止遍历,并返回当前已积累的结果
func EachResultReverseF ¶ added in v0.1.5
EachResultReverseF 与 EachResultReverse 的功能相同,但是 abort 被默认为 false
func EachResultReverseT ¶ added in v0.1.5
EachResultReverseT 与 EachResultReverse 的功能相同,但是 abort 被默认为 true
func EachResultT ¶ added in v0.1.5
EachResultT 与 EachResult 的功能相同,但是 abort 被默认为 true
func EachReverse ¶ added in v0.1.5
EachReverse 根据传入的 abort 从后往前遍历 slice,如果 iterator 返回值与 abort 相同,则停止遍历
func EachReverseF ¶ added in v0.1.5
EachReverseF 与 EachReverse 的功能相同,但是 abort 被默认为 false
func EachReverseT ¶ added in v0.1.5
EachReverseT 与 EachReverse 的功能相同,但是 abort 被默认为 true
func FillByCopy ¶ added in v0.1.5
FillByCopy 与 FillBy 的功能相同,但是不会改变原切片,而是返回一个新的切片
func FillUntil ¶ added in v0.1.5
FillUntil 用指定的值填充切片,如果 fn 返回的 bool 值与 abort 相同,则停止填充
- abort: 填充中止条件
- slice: 待填充的切片
func FillUntilCopy ¶ added in v0.1.5
FillUntilCopy 与 FillUntil 的功能相同,但不会改变原切片,而是返回一个新的切片
func FillUntilF ¶ added in v0.1.5
FillUntilF 是 FillUntil 的简化版本,其中 abort 参数为 false
func FillUntilFCopy ¶ added in v0.1.5
FillUntilFCopy 是 FillUntilCopy 的简化版本,其中 abort 参数为 false
func FillUntilT ¶ added in v0.1.5
FillUntilT 是 FillUntil 的简化版本,其中 abort 参数为 true
func FillUntilTCopy ¶ added in v0.1.5
FillUntilTCopy 是 FillUntilCopy 的简化版本,其中 abort 参数为 true
func Filter ¶ added in v0.0.31
Filter 根据特定的表达式过滤切片成员
- reserve: 是否保留符合条件的成员
- slice: 待过滤的切片
- expression: 过滤表达式
这个函数的作用是遍历输入切片 slice,然后根据 expression 函数的返回值来决定是否保留每个元素。具体来说
- 如果 expression 返回 true 并且 reserve 也是 true,那么元素会被保留
- 如果 expression 返回 false 并且 reserve 是 false,那么元素也会被保留
该没有创建新的内存空间或进行元素复制,所以整个操作相当高效。同时,由于 s 和 slice 实际上共享底层的数组,因此这个函数会改变传入的 slice。如果不希望改变原切片,需要在函数调用之前手动复制一份或者使用 FilterCopy 函数。
func FilterCopy ¶ added in v0.1.5
FilterCopy 与 Filter 的功能相同,但是不会改变原切片,而是返回一个新的切片
func FilterFCopy ¶ added in v0.1.5
FilterFCopy 与 FilterCopy 的功能相同,但是 reserve 被默认为 false
func FilterTCopy ¶ added in v0.1.5
FilterTCopy 与 FilterCopy 的功能相同,但是 reserve 被默认为 true
func GetEndPart ¶ added in v0.0.14
GetEndPart 获取数组的后 n 个元素
func GetIndex ¶ added in v0.0.20
func GetIndex[V comparable](slice []V, value V) int
GetIndex 判断数组是否包含某个元素,如果包含则返回索引
func GetIndexAny ¶ added in v0.0.20
GetIndexAny 判断数组是否包含某个元素,如果包含则返回索引
func GetStartPart ¶ added in v0.0.14
GetStartPart 获取数组的前 n 个元素
func GetValueHandle ¶ added in v0.0.30
GetValueHandle 获取特定索引的元素,并通过 handle 进入传入,如果索引超出范围则不会进入 handle
func GroupBy ¶ added in v0.1.5
func GroupBy[T any, K comparable](collection []T, fn func(T) K) map[K][]T
GroupBy 返回分组后的切片
func IsIntersectWithCheck ¶ added in v0.0.20
IsIntersectWithCheck 判断两个切片是否有交集
func LimitedCombinations ¶ added in v0.0.20
LimitedCombinations 获取给定数组的所有组合,且每个组合的成员数量限制在指定范围内
func Map ¶ added in v0.1.5
Map 将切片中的每一个元素转换为另一种类型的元素
- slice: 待转换的切片
- converter: 转换函数
这不会改变原有的切片,而是返回一个新的切片
func Reduce ¶ added in v0.1.5
func Reduce[V any, R generic.Number](start int, slice []V, reducer func(index int, item V, current R) R) (result R)
Reduce 将切片中的多个元素组合成一个单一值
- start: 开始索引,如果为负数则从后往前计算,例如:-1 表示从最后一个元素开始向左遍历,1 表示从第二个元素开始
- slice: 待组合的切片
- reducer: 组合函数
func Shuffle ¶ added in v0.0.5
func Shuffle[T any](collection []T) []T
Shuffle 随机打乱切片
- 该函数会改变传入的切片,如果不希望改变原切片,需要在函数调用之前手动复制一份或者使用 ShuffleCopy 函数
func ShuffleCopy ¶ added in v0.1.5
func ShuffleCopy[T any](collection []T) []T
ShuffleCopy 返回随机打乱后的切片
- 该函数不会改变原切片
func ToMap ¶ added in v0.0.5
func ToMap[K comparable, V any](slice []V, key func(V) K) map[K]V
ToMap 将数组转换为 map
Types ¶
type Priority ¶ added in v0.0.30
type Priority[V any] struct { // contains filtered or unexported fields }
Priority 是一个优先级切片
func NewPriority ¶ added in v0.0.30
NewPriority 创建一个优先级切片
func (*Priority[V]) Action ¶ added in v0.0.30
func (slf *Priority[V]) Action(action func(items []*PriorityItem[V]) []*PriorityItem[V])
Action 直接操作切片,如果返回值不为 nil,则替换切片
func (*Priority[V]) Get ¶ added in v0.0.30
func (slf *Priority[V]) Get(index int) *PriorityItem[V]
Get 获取元素
func (*Priority[V]) GetPriority ¶ added in v0.0.30
GetPriority 获取元素优先级
func (*Priority[V]) Range ¶ added in v0.0.30
func (slf *Priority[V]) Range(action func(index int, item *PriorityItem[V]) bool)
Range 遍历切片,如果返回值为 false,则停止遍历
func (*Priority[V]) RangePriority ¶ added in v0.0.30
RangePriority 遍历切片优先级,如果返回值为 false,则停止遍历
func (*Priority[V]) RangeValue ¶ added in v0.0.30
RangeValue 遍历切片值,如果返回值为 false,则停止遍历
func (*Priority[V]) SetPriority ¶ added in v0.0.30
SetPriority 设置元素优先级
type PriorityItem ¶ added in v0.0.30
type PriorityItem[V any] struct { // contains filtered or unexported fields }
PriorityItem 是一个优先级切片元素
func NewPriorityItem ¶ added in v0.0.30
func NewPriorityItem[V any](v V, priority int) *PriorityItem[V]
NewPriorityItem 创建一个优先级切片元素
func (*PriorityItem[V]) Next ¶ added in v0.0.30
func (p *PriorityItem[V]) Next() *PriorityItem[V]
Next 返回下一个元素
func (*PriorityItem[V]) Prev ¶ added in v0.0.30
func (p *PriorityItem[V]) Prev() *PriorityItem[V]
Prev 返回上一个元素
func (*PriorityItem[V]) Priority ¶ added in v0.0.30
func (p *PriorityItem[V]) Priority() int
Priority 返回元素优先级