slicex

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Collect

func Collect[T any, M comparable](ts []T, f func(t T) M) []M

func Each

func Each[T any](ts []T, f func(t T))

func Eachv

func Eachv[T any, V any](ts []T, f func(t T) V) []V

func Equal

func Equal[E comparable](s1, s2 []E) bool

Equal reports whether two slices are equal: the same length and all elements equal. If the lengths are different, Equal returns false. Otherwise, the elements are compared in increasing index order, and the comparison stops at the first unequal pair. Floating point NaNs are not considered equal.

func EqualFunc

func EqualFunc[E1, E2 any](s1 []E1, s2 []E2, eq func(E1, E2) bool) bool

EqualFunc reports whether two slices are equal using a comparison function on each pair of elements. If the lengths are different, EqualFunc returns false. Otherwise, the elements are compared in increasing index order, and the comparison stops at the first index for which eq returns false.

func Map

func Map[K comparable, V any](vs []V, f func(v V) K) map[K]V

func Mapv

func Mapv[K comparable, V any, T any](ts []T, f func(t T) (K, V)) map[K]V

Types

type S

type S[T comparable] []T

S is a generic slice type.

func New

func New[T comparable]() S[T]

func NewSize

func NewSize[T comparable](size int) S[T]

func Wrap

func Wrap[T comparable](s []T) S[T]

func (S[T]) Clip

func (s S[T]) Clip() S[T]

Clip removes unused capacity from the slice, returning s[:len(s):len(s)].

func (S[T]) Contain

func (s S[T]) Contain(e T) bool

func (S[T]) Delete

func (s S[T]) Delete(elem ...T) S[T]

Delete will delete the give elements.

func (S[T]) Diff

func (s S[T]) Diff(dest []T) S[T]

Diff returns the different elements between source and dest slice.

func (S[T]) Filter

func (s S[T]) Filter(cond func(a T) bool) S[T]

Filter is a method that opposite to the Retain method, it will filter the elements that not matched the condition the function param defined, and not matched elements will be retained. This method will return a new slice and the original slice will not be changed

func (S[T]) Intersect

func (s S[T]) Intersect(dest []T) S[T]

Intersect returns those elements that both the source and the dest slice have. i.e. the result is and intersection set.

func (S[T]) Join

func (s S[T]) Join(sep string) string

Join is a method that join all the elements by the string the sep param defined.

func (S[T]) Raw

func (s S[T]) Raw() []T

func (S[T]) Remove

func (s S[T]) Remove(dest []T) S[T]

Remove method will remove elements which the dest slice have from the source slice.

func (S[T]) RemoveDuplicate

func (s S[T]) RemoveDuplicate() S[T]

func (S[T]) Retain

func (s S[T]) Retain(cond func(a T) bool) S[T]

Retain is a method that retain the elements that matched the condition the function param defined, and not matched elements will be removed. This method will return a new slice and the original slice will not be changed

func (S[T]) Sort

func (s S[T]) Sort(less func(x, y T) bool) SortableSlice[T]

Sort is a method to sort the original slice by the given argument less, which is a method to define how to compare the elements in it, this less method receive two T type so Sort method will NOT change the original slice. Sort will return a SortableSlice instance, so you could invoke its other api such as SortableSlice.Reverse etc.

func (S[T]) Union

func (s S[T]) Union(dest []T) S[T]

Union returns all elements of the two slices, i.e. the result is and union set.

type SortableSlice

type SortableSlice[T comparable] struct {
	// contains filtered or unexported fields
}

SortableSlice is a struct to define a sortable slice, it implements sort.Interface.

func (SortableSlice[T]) Len

func (s SortableSlice[T]) Len() int

func (SortableSlice[T]) Less

func (s SortableSlice[T]) Less(i, j int) bool

func (SortableSlice[T]) Raw added in v1.0.0

func (s SortableSlice[T]) Raw() []T

func (SortableSlice[T]) Reverse

func (s SortableSlice[T]) Reverse() S[T]

Reverse is a method to reverse the sequence that the Slice.Sort method sorted, so you should has invoked the S.Sort first before to call Reverse.

func (SortableSlice[T]) Swap

func (s SortableSlice[T]) Swap(i, j int)

Jump to

Keyboard shortcuts

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