slice

package
v2.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2022 License: MIT Imports: 5 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 Chunk

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

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

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 Count

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

Count iterates over elements of slice, returns a count 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(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

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

func InterfaceSlice

func InterfaceSlice(slice any) []any

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

func StringSlice

func StringSlice(slice any) []string

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 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, values ...T) []T

Without creates a slice excluding all given values

Types

This section is empty.

Jump to

Keyboard shortcuts

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