slice

package
v0.0.0-...-1c003c8 Latest Latest
Warning

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

Go to latest
Published: May 10, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains[T comparable](slice []T, item T) bool

Contains Check item in slice T type

func ContainsBy

func ContainsBy[T any](slice []T, predicate func(T) bool) bool

ContainsBy returns true if predicate function return true.

func Diff

func Diff[T comparable, E any](sources []T, slices []E, f func(v E) T) []T

Diff Get []T in sources but not in slices Example:

slices.Diff(skus, products, func(p Product) string {
	return p.TkSku
})

func Every

func Every[T comparable](collection []T, subset []T) bool

Every returns true if all elements of a subset are contained into a collection or if the subset is empty.

func EveryBy

func EveryBy[T any](collection []T, predicate func(item T) bool) bool

EveryBy returns true if the predicate returns true for all of the elements in the collection or if the collection is empty.

func Filter

func Filter[U any](slices []U, predicate func(U) bool) []U

Filter iterating over elements of a collection, returning an array of all elements predicate returns truthy for.

func FilterIndex

func FilterIndex[U any](slices []U, predicate func(U, int) bool) []U

FilterIndex Filter with predicate index Filter handle index

func FilterMap

func FilterMap[T any, R any](slices []T, filterTrans func(item T) (bool, R)) []R

FilterMap returns a slice which obtained after both filtering and mapping using the given callback function. The callback function should return two values:

  • the result of the mapping operation and
  • whether the result element should be included or not.

Play: https://go.dev/play/p/-AuYXfy7opz

func First

func First[U any](slices []U, predicate func(U) bool) U

First iterates over elements of collection, returning first element returns truthy for.

func FlatMap

func FlatMap[U any, V any](sources []U, transform func(U) []V) []V

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

func FlatMapIndex

func FlatMapIndex[U any, V any](sources []U, transform func(U, int) []V) []V

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

func Flatten

func Flatten[T any](collection [][]T) []T

Flatten returns an array a single level deep. Play: https://go.dev/play/p/rbp9ORaMpjw

func ForEach

func ForEach[T any](collection []T, iteratee func(T))

ForEach iterates over elements of collection and invokes iteratee for each element.

func ForEachIndex

func ForEachIndex[T any](collection []T, iteratee func(T, int))

ForEachIndex iterates over elements of collection and invokes iteratee for each element. ForEach handle index

func GetAt

func GetAt[E any](slices []E, pos int) E

GetAt get element at pos of slices

func GroupBy

func GroupBy[U any, K comparable](slices []U, iteratee func(U) K) map[K][]U

GroupBy returns an object composed of keys generated from the results of running each element of collection through iteratee.

func GroupFlatMapBy

func GroupFlatMapBy[U any, K comparable, V any](slices []U, trans func(U) (K, []V)) map[K][]V

GroupFlatMapBy returns an object composed of keys generated from the results of running each element of collection through iteratee.

func GroupMapBy

func GroupMapBy[U any, K comparable, V any](slices []U, trans func(U) (K, V)) map[K][]V

GroupMapBy returns an object composed of keys generated from the results of running each element of collection through iteratee.

func IsEmpty

func IsEmpty[U any](slices []U) bool

IsEmpty check slices are empty

func IsNotEmpty

func IsNotEmpty[U any](slices []U) bool

IsNotEmpty check slices are not empty

func Key

func Key[U any, K comparable](sources []U, transform func(u U) K) map[K]U

Key transforms a slice or an array of structs to a map based on a pivot callback Example:

slices.Key(products, func(p Product) int64 {
	return p.ID
})

func KeyBy

func KeyBy[U any, K comparable, V any](sources []U, transform func(u U) (K, V)) map[K]V

KeyBy transforms a slice or an array of structs to a map based on a pivot callback Example:

slices.KeyBy(products, func(p Product) (int64, string) {
	return p.ID, p.TkSku
})

func Make

func Make[T any](values ...T) []T

func Map

func Map[U any, V any](sources []U, transfer func(u U) V) []V

Map Convert []U to []V with func transform Example:

slices.Map(products, func(p Product) []int64 {
	return p.ID
})

func MapDistinct

func MapDistinct[U any, V comparable](sources []U, transfer func(u U) V) []V

MapDistinct Map and DISTINCT

func MapIndex

func MapIndex[U any, V any](sources []U, transform func(U, int) V) []V

MapIndex Convert []U to []V with func transform Map handle index Example:

slices.Map2(products, func(p Product, index int) []int64 {
	return p.ID
})

func Max

func Max[T constraints.Ordered](collection []T) T

Max searches the maximum value of a collection. Returns zero value when collection is empty.

func MaxBy

func MaxBy[T any](collection []T, comparison func(a T, b T) bool) T

MaxBy search the maximum value of a collection using the given comparison function. If several values of the collection are equal to the greatest value, returns the first such value. Returns zero value when collection is empty.

func Min

func Min[T constraints.Ordered](collection []T) T

Min search the minimum value of a collection. Returns zero value when collection is empty.

func MinBy

func MinBy[T any](collection []T, comparison func(a T, b T) bool) T

MinBy search the minimum value of a collection using the given comparison function. If several values of the collection are equal to the smallest value, returns the first such value. Returns zero value when collection is empty.

func Nth

func Nth[T any, N constraints.Integer](collection []T, nth N) (T, error)

Nth returns the element at index `nth` of collection. If `nth` is negative, the nth element from the end is returned. An error is returned when nth is out of slice bounds.

func Reduce

func Reduce[T any, R any](collection []T, accumulator func(R, T, int) R, initial R) R

Reduce reduces collection to a value which is the accumulated result of running each element in collection through accumulator, where each successive invocation is supplied the return value of the previous.

func ToFlatMap

func ToFlatMap[U any, K comparable, V any](sources []U, transform func(u U) (K, V)) map[K][]V

ToFlatMap convert []U to map[K][]V Example:

collection.MakeAndMergeMap(products, func(p Product) (int64, string) {
	return p.ID, p.TkSku
})

func Uniq

func Uniq[T comparable](slices []T) []T

Uniq returns a duplicate-free version of an array, in which only the first occurrence of each element is kept. The order of result values is determined by the order they occur in the array.

func UniqBy

func UniqBy[T any, U comparable](slices []T, iteratee func(T) U) []T

UniqBy returns a duplicate-free version of an array, in which only the first occurrence of each element is kept. The order of result values is determined by the order they occur in the array. It accepts `iteratee` which is invoked for each element in array to generate the criterion by which uniqueness is computed.

Types

This section is empty.

Jump to

Keyboard shortcuts

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