Documentation
¶
Overview ¶
Package slice implements some functions to manipulate slice.
Index ¶
- func Chunk[T any](slice []T, size int) [][]T
- func Compact[T any](slice []T) []T
- func Concat[T any](slice []T, values ...[]T) []T
- func Contain[T comparable](slice []T, value T) bool
- func ContainSubSlice[T comparable](slice, subslice []T) bool
- func Count[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(value, otherValue 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(T) U) map[U][]T
- func IndexOf[T comparable](slice []T, value 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 LastIndexOf[T comparable](slice []T, value T) int
- func Map[T any, U any](slice []T, iteratee func(index int, item T) U) []U
- 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 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 SortByField(slice any, field string, sortType ...string) error
- func StringSlice(slice any) []string
- func SymmetricDifference[T comparable](slices ...[]T) []T
- func ToSlice[T any](value ...T) []T
- func ToSlicePointer[T any](value ...T) []*T
- func Union[T comparable](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, values ...T) []T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Compact ¶
func Compact[T any](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, values ...[]T) []T
Concat creates a new slice concatenating slice with any additional slices and/or values.
func Contain ¶
func Contain[T comparable](slice []T, value T) bool
Contain check if the value is in the slice or not
func ContainSubSlice ¶
func ContainSubSlice[T comparable](slice, subslice []T) bool
ContainSubSlice check if the slice contain subslice or not
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 ¶
func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(value, otherValue T) bool) []T
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(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, value T) int
IndexOf returns the index at which the first occurrence of a value is found in a slice or return -1 if the value cannot be found.
func InterfaceSlice ¶
InterfaceSlice convert param to slice of interface.
func Intersection ¶
func Intersection[T comparable](slices ...[]T) []T
Intersection creates a slice of unique values that included by all slices.
func LastIndexOf ¶ added in v2.0.7
func LastIndexOf[T comparable](slice []T, value T) int
LastIndexOf returns the index at which the last occurrence of a value is found in a slice or return -1 if the value cannot be found.
func Reduce ¶
Reduce creates an slice of values by running each element of slice thru iteratee function.
func Reverse ¶
func Reverse[T any](slice []T)
Reverse return slice of element order is reversed to the given slice
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
func StringSlice ¶
StringSlice convert param to slice of string.
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](value ...T) []T
ToSlice returns a slices of a variable parameter transformation
func ToSlicePointer ¶ added in v2.1.1
func ToSlicePointer[T any](value ...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 values, in order, from all given slices. using == for equality comparisons.
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, values ...T) []T
Without creates a slice excluding all given values
Types ¶
This section is empty.