Documentation
¶
Overview ¶
Package slice implements some functions to manipulate slice.
Index ¶
- func AppendIfAbsent[T comparable](slice []T, item T) []T
- func Chunk[T any](slice []T, size int) [][]T
- func Compact[T comparable](slice []T) []T
- func Concat[T any](slice []T, slices ...[]T) []T
- func Contain[T comparable](slice []T, target T) bool
- func ContainSubSlice[T comparable](slice, subSlice []T) bool
- func Count[T comparable](slice []T, item T) int
- func CountBy[T any](slice []T, predicate func(index int, item T) bool) int
- func DeleteAt[T any](slice []T, start int, end ...int) []T
- func Difference[T comparable](slice, comparedSlice []T) []T
- func DifferenceBy[T comparable](slice []T, comparedSlice []T, iteratee func(index int, item T) T) []T
- func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(item1, item2 T) bool) []T
- func Drop[T any](slice []T, n int) []T
- func Equal[T comparable](slice1, slice2 []T) bool
- func EqualWith[T, U any](slice1 []T, slice2 []U, comparator func(T, U) bool) bool
- func Every[T any](slice []T, predicate func(index int, item T) bool) bool
- func Filter[T any](slice []T, predicate func(index int, item T) bool) []T
- func Find[T any](slice []T, predicate func(index int, item T) bool) (*T, bool)
- func FindLast[T any](slice []T, predicate func(index int, item T) bool) (*T, bool)
- func Flatten(slice any) any
- func FlattenDeep(slice any) any
- func ForEach[T any](slice []T, iteratee func(index int, item T))
- func GroupBy[T any](slice []T, groupFn func(index int, item T) bool) ([]T, []T)
- func GroupWith[T any, U comparable](slice []T, iteratee func(item T) U) map[U][]T
- func IndexOf[T comparable](slice []T, item T) int
- func InsertAt[T any](slice []T, index int, value any) []T
- func IntSlice(slice any) []int
- func InterfaceSlice(slice any) []any
- func Intersection[T comparable](slices ...[]T) []T
- func KeyBy[T any, U comparable](slice []T, iteratee func(item T) U) map[U]T
- func LastIndexOf[T comparable](slice []T, item T) int
- func Map[T any, U any](slice []T, iteratee func(index int, item T) U) []U
- func Merge[T any](slices ...[]T) []T
- func None[T any](slice []T, predicate func(index int, item T) bool) bool
- func Reduce[T any](slice []T, iteratee func(index int, item1, item2 T) T, initial T) T
- func Repeat[T any](item T, n int) []T
- func Replace[T comparable](slice []T, old T, new T, n int) []T
- func ReplaceAll[T comparable](slice []T, old T, new T) []T
- func Reverse[T any](slice []T)
- func Shuffle[T any](slice []T) []T
- func Some[T any](slice []T, predicate func(index int, item T) bool) bool
- func Sort[T lancetconstraints.Ordered](slice []T, sortOrder ...string)
- func SortBy[T any](slice []T, less func(a, b T) bool)
- func SortByField(slice any, field string, sortType ...string) error
- func StringSlice(slice any) []string
- func SymmetricDifference[T comparable](slices ...[]T) []T
- func ToSlice[T any](items ...T) []T
- func ToSlicePointer[T any](items ...T) []*T
- func Union[T comparable](slices ...[]T) []T
- func UnionBy[T any, V comparable](predicate func(item T) V, slices ...[]T) []T
- func Unique[T comparable](slice []T) []T
- func UniqueBy[T comparable](slice []T, iteratee func(item T) T) []T
- func UpdateAt[T any](slice []T, index int, value T) []T
- func Without[T comparable](slice []T, items ...T) []T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendIfAbsent ¶ added in v2.1.4
func AppendIfAbsent[T comparable](slice []T, item T) []T
AppendIfAbsent only absent append the item
func Compact ¶
func Compact[T comparable](slice []T) []T
Compact creates an slice with all falsey values removed. The values false, nil, 0, and "" are falsey
func Concat ¶
func Concat[T any](slice []T, slices ...[]T) []T
Concat creates a new slice concatenating slice with any additional slices.
func Contain ¶
func Contain[T comparable](slice []T, target T) bool
Contain check if the target value is in the slice or not
func ContainSubSlice ¶
func ContainSubSlice[T comparable](slice, subSlice []T) bool
ContainSubSlice check if the slice contain a given subslice or not
func Count ¶
func Count[T comparable](slice []T, item T) int
Count returns the number of occurrences of the given item in the slice
func CountBy ¶ added in v2.1.11
CountBy iterates over elements of slice with predicate function, returns the number of all matched elements
func Difference ¶
func Difference[T comparable](slice, comparedSlice []T) []T
Difference creates an slice of whose element in slice but not in comparedSlice
func DifferenceBy ¶
func DifferenceBy[T comparable](slice []T, comparedSlice []T, iteratee func(index int, item T) T) []T
DifferenceBy it accepts iteratee which is invoked for each element of slice and values to generate the criterion by which they're compared. like lodash.js differenceBy: https://lodash.com/docs/4.17.15#differenceBy,
func DifferenceWith ¶
DifferenceWith accepts comparator which is invoked to compare elements of slice to values. The order and references of result values are determined by the first slice. The comparator is invoked with two arguments: (arrVal, othVal).
func Drop ¶
Drop creates a slice with `n` elements dropped from the beginning when n > 0, or `n` elements dropped from the ending when n < 0
func Equal ¶ added in v2.0.9
func Equal[T comparable](slice1, slice2 []T) bool
Equal checks if two slices are equal: the same length and all elements' order and value are equal
func Filter ¶
Filter iterates over elements of slice, returning an slice of all elements pass the predicate function
func Find ¶
Find iterates over elements of slice, returning the first one that passes a truth test on predicate function. If return T is nil then no items matched the predicate func
func FindLast ¶
FindLast iterates over elements of slice from end to begin, returning the first one that passes a truth test on predicate function. If return T is nil then no items matched the predicate func
func GroupBy ¶
GroupBy iterate over elements of the slice, each element will be group by criteria, returns two slices
func GroupWith ¶ added in v2.0.1
func GroupWith[T any, U comparable](slice []T, iteratee func(item T) U) map[U][]T
GroupWith return a map composed of keys generated from the resultults of running each element of slice thru iteratee.
func IndexOf ¶ added in v2.0.7
func IndexOf[T comparable](slice []T, item T) int
IndexOf returns the index at which the first occurrence of a item is found in a slice or return -1 if the item cannot be found.
func IntSlice ¶
IntSlice convert param to slice of int. This function is deprecated, use generics feature of go1.18+ for replacement
func InterfaceSlice ¶
InterfaceSlice convert param to slice of interface. This function is deprecated, use generics feature of go1.18+ for replacement
func Intersection ¶
func Intersection[T comparable](slices ...[]T) []T
Intersection creates a slice of unique elements that included by all slices.
func KeyBy ¶ added in v2.1.9
func KeyBy[T any, U comparable](slice []T, iteratee func(item T) U) map[U]T
KeyBy converts a slice to a map based on a callback function
func LastIndexOf ¶ added in v2.0.7
func LastIndexOf[T comparable](slice []T, item T) int
LastIndexOf returns the index at which the last occurrence of the item is found in a slice or return -1 if the then cannot be found.
func Merge ¶ added in v2.1.11
func Merge[T any](slices ...[]T) []T
Merge all given slices into one slice
func Reduce ¶
Reduce creates an slice of values by running each element of slice thru iteratee function.
func Repeat ¶ added in v2.1.11
Repeat creates a slice with length n whose elements are param `item`.
func Replace ¶ added in v2.1.8
func Replace[T comparable](slice []T, old T, new T, n int) []T
Replace returns a copy of the slice with the first n non-overlapping instances of old replaced by new
func ReplaceAll ¶ added in v2.1.8
func ReplaceAll[T comparable](slice []T, old T, new T) []T
ReplaceAll returns a copy of the slice with all non-overlapping instances of old replaced by new.
func Reverse ¶
func Reverse[T any](slice []T)
Reverse return slice of element order is reversed to the given slice
func Sort ¶ added in v2.1.11
func Sort[T lancetconstraints.Ordered](slice []T, sortOrder ...string)
Sort sorts a slice of any ordered type(number or string), use quick sort algrithm. default sort order is ascending (asc), if want descending order, set param `sortOrder` to `desc`
func SortBy ¶ added in v2.1.11
SortBy sorts the slice in ascending order as determined by the less function. This sort is not guaranteed to be stable
func SortByField ¶
SortByField return sorted slice by field slice element should be struct, field type should be int, uint, string, or bool default sortType is ascending (asc), if descending order, set sortType to desc This function is deprecated, use Sort and SortBy for replacement
func StringSlice ¶
StringSlice convert param to slice of string. This function is deprecated, use generics feature of go1.18+ for replacement
func SymmetricDifference ¶ added in v2.0.3
func SymmetricDifference[T comparable](slices ...[]T) []T
SymmetricDifference oppoiste operation of intersection function
func ToSlice ¶ added in v2.1.1
func ToSlice[T any](items ...T) []T
ToSlice returns a slices of a variable parameter transformation
func ToSlicePointer ¶ added in v2.1.1
func ToSlicePointer[T any](items ...T) []*T
ToSlicePointer returns a pointer to the slices of a variable parameter transformation
func Union ¶
func Union[T comparable](slices ...[]T) []T
Union creates a slice of unique elements, in order, from all given slices.
func UnionBy ¶ added in v2.1.9
func UnionBy[T any, V comparable](predicate func(item T) V, slices ...[]T) []T
UnionBy is like Union, what's more it accepts iteratee which is invoked for each element of each slice
func UniqueBy ¶ added in v2.1.0
func UniqueBy[T comparable](slice []T, iteratee func(item T) T) []T
UniqueBy call iteratee func with every item of slice, then remove duplicated.
func Without ¶
func Without[T comparable](slice []T, items ...T) []T
Without creates a slice excluding all given items
Types ¶
This section is empty.