slice

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendIfAbsent

func AppendIfAbsent[T comparable](slice []T, item T) []T

AppendIfAbsent only absent append the item.

func Chunk

func Chunk[T any](slice []T, size int) [][]T

Chunk creates a slice of elements split into groups the length of size.

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 ContainBy

func ContainBy[T any](slice []T, predicate func(item T) bool) bool

ContainBy returns true if predicate function return true.

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

func CountBy[T any](slice []T, predicate func(index int, item T) bool) int

CountBy iterates over elements of slice with predicate function, returns the number of all matched elements.

func DeleteAt

func DeleteAt[T any](slice []T, start int, end ...int) []T

DeleteAt delete the element of slice from start index to end index - 1. Play: https://go.dev/play/p/pJ-d6MUWcvK

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.

func DifferenceWith

func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(item1, item2 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

func Drop[T any](slice []T, n int) []T

Drop drop n elements from the start of a slice. Play: https://go.dev/play/p/jnPO2yQsT8H

func DropRight

func DropRight[T any](slice []T, n int) []T

DropRight drop n elements from the end of a slice. Play: https://go.dev/play/p/8bcXvywZezG

func DropRightWhile

func DropRightWhile[T any](slice []T, predicate func(item T) bool) []T

DropRightWhile drop n elements from the end of a slice while predicate function returns true. Play: https://go.dev/play/p/6wyK3zMY56e

func DropWhile

func DropWhile[T any](slice []T, predicate func(item T) bool) []T

DropWhile drop n elements from the start of a slice while predicate function returns true. Play: https://go.dev/play/p/4rt252UV_qs

func Equal

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 EqualWith

func EqualWith[T, U any](slice1 []T, slice2 []U, comparator func(T, U) bool) bool

EqualWith checks if two slices are equal with comparator func.

func Every

func Every[T any](slice []T, predicate func(index int, item T) bool) bool

Every return true if all of the values in the slice pass the predicate function.

func Filter

func Filter[T any](slice []T, predicate func(index int, item T) bool) []T

Filter iterates over elements of slice, returning an slice of all elements pass the predicate function.

func FilterMap

func FilterMap[T any, U any](slice []T, iteratee func(index int, item T) (U, bool)) []U

FilterMap returns a slice which apply both filtering and mapping to the given slice. iteratee callback function should returntwo values: 1, mapping result. 2, whether the result element should be included or not

func Find

func Find[T any](slice []T, predicate func(index int, item T) bool) (*T, bool)

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 FindBy

func FindBy[T any](slice []T, predicate func(index int, item T) bool) (v T, ok bool)

FindBy iterates over elements of slice, returning the first one that passes a truth test on predicate function. If return T is nil or zero value then no items matched the predicate func. In contrast to Find or FindLast, its return value no longer requires dereferencing

func FindLast

func FindLast[T any](slice []T, predicate func(index int, item T) bool) (*T, bool)

FindLast iterates over elements of slice from end to begin, return the first one that passes a truth test on predicate function. If return T is nil then no items matched the predicate func.

func FindLastBy

func FindLastBy[T any](slice []T, predicate func(index int, item T) bool) (v T, ok bool)

FindLastBy iterates over elements of slice, returning the last one that passes a truth test on predicate function. If return T is nil or zero value then no items matched the predicate func. In contrast to Find or FindLast, its return value no longer requires dereferencing

func FlatMap

func FlatMap[T any, U any](slice []T, iteratee func(index int, item T) []U) []U

FlatMap manipulates a slice and transforms and flattens it to a slice of another type.

func Flatten

func Flatten(slice any) any

Flatten flattens slice with one level.

func FlattenDeep

func FlattenDeep(slice any) any

FlattenDeep flattens slice recursive.

func ForEach

func ForEach[T any](slice []T, iteratee func(index int, item T))

ForEach iterates over elements of slice and invokes function for each element.

func ForEachWithBreak

func ForEachWithBreak[T any](slice []T, iteratee func(index int, item T) bool)

ForEachWithBreak iterates over elements of slice and invokes function for each element, when iteratee return false, will break the for each loop.

func GroupBy

func GroupBy[T any](slice []T, groupFn func(index int, item T) bool) ([]T, []T)

GroupBy iterate over elements of the slice, each element will be group by criteria, returns two slices.

func GroupWith

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

func IndexOf[T comparable](arr []T, val T) int

IndexOf returns the index at which the first occurrence of an item is found in a slice or return -1 if the item cannot be found.

func InsertAt

func InsertAt[T any](slice []T, index int, value any) []T

InsertAt insert the value or other slice into slice at index. Play: https://go.dev/play/p/hMLNxPEGJVE

func IntSlice

func IntSlice(slice any) []int

IntSlice convert param to slice of int. This function is deprecated, use generics feature of go1.18+ for replacement. Play: https://go.dev/play/p/UQDj-on9TGN

func InterfaceSlice

func InterfaceSlice(slice any) []any

InterfaceSlice convert param to slice of interface. This function is deprecated, use generics feature of go1.18+ for replacement. Play: https://go.dev/play/p/FdQXF0Vvqs-

func Intersection

func Intersection[T comparable](slices ...[]T) []T

Intersection creates a slice of unique elements that included by all slices. Play: https://go.dev/play/p/anJXfB5wq_t

func IsAscending

func IsAscending[T constraints.Ordered](slice []T) bool

IsAscending checks if a slice is ascending order.

func IsDescending

func IsDescending[T constraints.Ordered](slice []T) bool

IsDescending checks if a slice is descending order.

func IsSorted

func IsSorted[T constraints.Ordered](slice []T) bool

IsSorted checks if a slice is sorted(ascending or descending).

func IsSortedByKey

func IsSortedByKey[T any, K constraints.Ordered](slice []T, iteratee func(item T) K) bool

IsSortedByKey checks if a slice is sorted by iteratee function.

func Join

func Join[T any](s []T, separator string) string

Join the slice item with specify separator.

func KeyBy

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

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 Map

func Map[T any, U any](slice []T, iteratee func(index int, item T) U) []U

Map creates an slice of values by running each element of slice thru iteratee function.

func Merge

func Merge[T any](slices ...[]T) []T

Merge all given slices into one slice. Play: https://go.dev/play/p/lbjFp784r9N

func None

func None[T any](slice []T, predicate func(index int, item T) bool) bool

None return true if all the values in the slice mismatch the criteria.

func Reduce

func Reduce[T any](slice []T, iteratee func(index int, item1, item2 T) T, initial T) T

Reduce creates an slice of values by running each element of slice thru iteratee function.

func ReduceBy

func ReduceBy[T any, U any](slice []T, initial U, reducer func(index int, item T, agg U) U) U

ReduceBy produces a value from slice by accumulating the result of each element as passed through the reducer function.

func ReduceRight

func ReduceRight[T any, U any](slice []T, initial U, reducer func(index int, item T, agg U) U) U

ReduceRight is like ReduceBy, but it iterates over elements of slice from right to left.

func Repeat

func Repeat[T any](item T, n int) []T

Repeat creates a slice with length n whose elements are param `item`. Play: https://go.dev/play/p/1CbOmtgILUU

func Replace

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. Play: https://go.dev/play/p/P5mZp7IhOFo

func ReplaceAll

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. Play: https://go.dev/play/p/CzqXMsuYUrx

func Reverse

func Reverse[T any](slice []T)

Reverse return slice of element order is reversed to the given slice. Play: https://go.dev/play/p/8uI8f1lwNrQ

func Shuffle

func Shuffle[T any](slice []T) []T

Shuffle the slice.

func Some

func Some[T any](slice []T, predicate func(index int, item T) bool) bool

Some return true if any of the values in the list pass the predicate function.

func Sort

func Sort[T constraints.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

func SortBy[T any](slice []T, less func(a, b T) bool)

SortBy sorts the slice in ascending order as determined by the less function. This sort is not guaranteed to be stable.

func SortByField

func SortByField(slice any, field string, sortType ...string) error

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

func StringSlice(slice any) []string

StringSlice convert param to slice of string. This function is deprecated, use generics feature of go1.18+ for replacement. Play: https://go.dev/play/p/W0TZDWCPFcI

func SymmetricDifference

func SymmetricDifference[T comparable](slices ...[]T) []T

SymmetricDifference oppoiste operation of intersection function. Play: https://go.dev/play/p/h42nJX5xMln

func ToSlice

func ToSlice[T any](items ...T) []T

ToSlice returns a slices of a variable parameter transformation.

func ToSlicePointer

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. Play: https://go.dev/play/p/hfXV1iRIZOf

func UnionBy

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. Play: https://go.dev/play/p/HGKHfxKQsFi

func Unique

func Unique[T comparable](slice []T) []T

Unique remove duplicate elements in slice. Play: https://go.dev/play/p/AXw0R3ZTE6a

func UniqueBy

func UniqueBy[T comparable](slice []T, iteratee func(item T) T) []T

UniqueBy call iteratee func with every item of slice, then remove duplicated. Play: https://go.dev/play/p/UR323iZLDpv

func UpdateAt

func UpdateAt[T any](slice []T, index int, value T) []T

UpdateAt update the slice element at index. Play: https://go.dev/play/p/f3mh2KloWVm

func Without

func Without[T comparable](slice []T, items ...T) []T

Without creates a slice excluding all given items.

Types

This section is empty.

Jump to

Keyboard shortcuts

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