slice

package
v2.1.11 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2022 License: MIT Imports: 6 Imported by: 178

Documentation

Overview

Package slice implements some functions to manipulate slice.

Index

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 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 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

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.

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(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 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 EqualWith added in v2.0.9

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 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 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, 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 Flatten added in v2.1.3

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 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 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 InsertAt

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

InsertAt insert the value or other slice into slice at index.

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

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

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 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 added in v2.1.11

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

Merge all given slices into one slice

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 Repeat added in v2.1.11

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

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 Shuffle

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

Shuffle creates an slice of shuffled values

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 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

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

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 Unique

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

Unique remove duplicate elements in 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 UpdateAt

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

UpdateAt update the slice element at index.

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