slicex

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2025 License: MIT Imports: 5 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendFirst

func AppendFirst[S ~[]E, E any](s S, e E) S

func AppendIfNotContains

func AppendIfNotContains[S ~[]E, E comparable](s S, v E) S

func AppendUnique

func AppendUnique[S ~[]E, E comparable](s S, v E) S

AppendUnique appends an element to a slice, if the element is not already in the slice

func Chunk

func Chunk[S ~[]E, E any](s S, size int) []S

func Concat

func Concat[S ~[]E, E any](ss ...S) S

func ContainsAny

func ContainsAny[E comparable](s []E, vs ...E) bool

ContainsAny checks if any of the elem are in the given slice.

func Difference

func Difference[S ~[]E, E comparable](s1 S, s2 S) S

Difference 返回差集 Deprecated: Do not use.

func Filter

func Filter[S ~[]E, E any](s S, f func(int, E) bool) S

func FindFunc

func FindFunc[E any](s []E, f func(E) bool) (E, bool)

func GroupBy

func GroupBy[M ~map[K]S, S ~[]E, E any, K comparable](s S, f func(int, E) K) M

GroupBy 函数将输入切片中的元素按照指定函数分组,并返回一个Map,其中键是分组的依据,值是对应元素的列表。

func IndexOrDefault

func IndexOrDefault[S ~[]E, E any](s S, index int, d E) E

func Indexes

func Indexes[E comparable](s []E, v E) []int

func IndexesFunc

func IndexesFunc[E any](s []E, f func(E) bool) []int

func Insert

func Insert[S ~[]E, E any](s S, i int, e E) S

Insert 在切片的指定位置插入元素

func IsEmpty

func IsEmpty[S ~[]E, E any](s S) bool

IsEmpty Checks if an slice is nil or length equals 0

func IsNotEmpty

func IsNotEmpty[S ~[]E, E any](s S) bool

func IsSameLength

func IsSameLength[S ~[]E, E any](s1 S, s2 S) bool

func LastIndex

func LastIndex[E comparable](s []E, v E) int

func Map

func Map[S1 ~[]E1, S2 ~[]E2, E1 any, E2 any](s S1, f func(int, E1) E2) S2

Map 方法创建一个新数组,这个新数组由原数组中的每个元素都调用一次提供的函数后的返回值组成。

func MapElem

func MapElem[S1 ~[]E1, S2 ~[]E2, E1 comparable, E2 any](v E1, s1 S1, s2 S2) E2

MapElem 两个切片s1,s2,,获取v所有s1同位置的s2的值

func Merge

func Merge[S ~[]E, E any](ss ...S) S

func NotContains

func NotContains[E comparable](s []E, v E) bool

NotContains reports whether v is not present in s.

func NotContainsFunc

func NotContainsFunc[E any](s []E, f func(E) bool) bool

NotContainsFunc reports whether v is not present in s.

func PadEnd

func PadEnd[S ~[]E, E any](s S, size int, val E) S

PadEnd 如果slice长度小于 length 则在右侧填充val。

func PadStart

func PadStart[S ~[]E, E any](s S, size int, val E) S

PadStart 如果slice长度小于 length 则在左侧填充val。

func Prepend

func Prepend[S ~[]E, E any](s S, elems ...E) S

func Reduce

func Reduce[S ~[]E, E any, R any](s S, initValue R, f func(previousValue R, currentValue E, currentIndex int, s S) R) R

func Remove

func Remove[S ~[]E, E comparable](s S, vs ...E) S

func RemoveAt

func RemoveAt[S ~[]E, E any](array S, is ...int) S

func RemoveFunc

func RemoveFunc[S ~[]E, E any](s S, f func(i int, v E) bool) S

func Reverse

func Reverse[S ~[]E, E any](s S) S

Reverse reverses the elements of the slice in place.

func SafeSlice

func SafeSlice[S ~[]E, E comparable](s S, start, length int) S

func SetAll

func SetAll[S ~[]E, E any](s S, f func(int) E) S

func Shift

func Shift[S ~[]E, E any](array S, offset int)

func Shifta

func Shifta[S ~[]E, E any](array S, startIndexInclusive, endIndexExclusive, offset int)

func Shuffle

func Shuffle[S ~[]E, E any](s S) S

Shuffle 打乱数组顺序

func Sum

func Sum[S ~[]E, E constraintx.Numeric](s S) E

Sum 数组求和

func Swap

func Swap[S ~[]E, E any](array S, offset1, offset2, length int)

Swap swaps a series of elements in the given array. array the array to swap. offset1 the index of the first element in the series to swap. offset2 the index of the second element in the series to swap. length the number of elements to swap starting with the given indices.

func ToMap

func ToMap[S ~[]E, M ~map[K]E, E any, K comparable](s S, f func(int, E) K) M

ToMap 方法创建一个Map,这个Map由原数组中的每个元素都调用一次提供的函数后的返回值作为Key、每个元素作为Value组成。

func ToSet

func ToSet[S ~[]E, M ~map[E]struct{}, E comparable](s S) M

func Uniq

func Uniq[S ~[]E, E comparable](s S) S

Uniq 用于去除切片中的重复元素并返回新切片。对于短切片,它通过逐个检查元素去重;对于长切片,使用Map提高效率。

Types

type SyncSlice

type SyncSlice[S ~[]E, E any] struct {
	// contains filtered or unexported fields
}

func NewSyncSlice

func NewSyncSlice[S ~[]E, E any]() *SyncSlice[S, E]

func WrapSlice

func WrapSlice[S ~[]E, E any](slice []E) *SyncSlice[S, E]

func (*SyncSlice[S, E]) Append

func (s *SyncSlice[S, E]) Append(elems ...E) *SyncSlice[S, E]

func (*SyncSlice[S, E]) Cap

func (s *SyncSlice[S, E]) Cap() int

func (*SyncSlice[S, E]) Index

func (s *SyncSlice[S, E]) Index(x int) E

func (*SyncSlice[S, E]) Len

func (s *SyncSlice[S, E]) Len() int

func (*SyncSlice[S, E]) Prepend

func (s *SyncSlice[S, E]) Prepend(elems ...E) *SyncSlice[S, E]

func (*SyncSlice[S, E]) Range

func (s *SyncSlice[S, E]) Range(f func(index int, elem E) bool)

func (*SyncSlice[S, E]) Slice

func (s *SyncSlice[S, E]) Slice(low int, high int, max ...int) *SyncSlice[S, E]

func (*SyncSlice[S, E]) Unwrap

func (s *SyncSlice[S, E]) Unwrap() []E

Jump to

Keyboard shortcuts

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