slicex

package
v0.1.13 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2025 License: MIT Imports: 6 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 raw, if the element is not already in the raw

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

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

ContainsAll checks if all of the elem are in the given raw.

func ContainsAny

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

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

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 raw 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 raw 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 RWMutexSlice added in v0.1.12

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

RWMutexSlice 是一个线程安全的切片。

func MakeSlice added in v0.1.12

func MakeSlice[S ~[]E, E any](length, capacity int) *RWMutexSlice[S, E]

func WrapSlice

func WrapSlice[S ~[]E, E any](raw []E) *RWMutexSlice[S, E]

func (*RWMutexSlice[S, E]) Append added in v0.1.12

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

Append 将一个或多个元素追加到切片的末尾。

func (*RWMutexSlice[S, E]) Cap added in v0.1.12

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

Cap 返回切片的容量。

func (*RWMutexSlice[S, E]) ClearAndUnwrap added in v0.1.12

func (s *RWMutexSlice[S, E]) ClearAndUnwrap() []E

ClearAndUnwrap 清空切片并返回原切片。

func (*RWMutexSlice[S, E]) Index added in v0.1.12

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

Index 返回切片中指定索引位置的元素。

func (*RWMutexSlice[S, E]) Len added in v0.1.12

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

Len 返回切片的长度。

func (*RWMutexSlice[S, E]) Prepend added in v0.1.12

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

Prepend 将一个或多个元素插入到切片的开头。

func (*RWMutexSlice[S, E]) Range added in v0.1.12

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

Range 遍历切片中的每个元素,并调用 f 函数。如果 f 返回 false,则停止遍历。

func (*RWMutexSlice[S, E]) Slice added in v0.1.12

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

Slice 返回一个新的切片,包含原切片中从 low 到 high 的元素。如果提供了 max,则返回的切片的容量为 max。

func (*RWMutexSlice[S, E]) Unwrap added in v0.1.12

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

Unwrap 返回一个新的切片,包含原切片中的所有元素。

Jump to

Keyboard shortcuts

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