sliceUtil

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2024 License: Apache-2.0 Imports: 0 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompareTwoSlices

func CompareTwoSlices[T comparable](listA, listB []T) bool

CompareTwoSlices asserts that the specified listA(array, slice...) is equal to specified listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, the number of appearances of each of them in both lists should match.

CompareTwoSlices([1, 3, 2, 3], [1, 3, 3, 2])

func ContainsStringAlias

func ContainsStringAlias[S ~[]E, E ~string](s S, v E) bool

ContainsStringAlias reports whether v is present in s.

func Difference

func Difference[T comparable](a, b []T) []T

Difference returns the elements in the slice `a` that aren't in slice `b`. Examples:

  • Difference([1, 2, 3, 4], [1, 2, 5]) => [3, 4]
  • Difference([1, 2, 3, 4], [1, 2, 3, 4]) => []
  • Difference([1, 2, 3, 4], [5, 6, 7]) => [1, 2, 3, 4]
  • Difference([1, 2, 3, 4], []) => [1, 2, 3, 4]
  • Difference([], [1, 2, 3, 4]) => []

func GetBeans

func GetBeans[T any](beans ...T) []T

GetBeans returns a slice of beans passed as arguments GetBeans(bean1, bean2, bean3) => []Bean{bean1, bean2, bean3} ...T is a variadic parameter that accepts a variable number of objects of type T. But the cost of using variadic parameters is that the compiler has to create a new slice and copy all the arguments into it. Note: Make sure to use this function only when the number of arguments is lower.

func GetBeansPtr

func GetBeansPtr[T any](beans ...*T) []*T

GetBeansPtr returns a slice of pointers to the beans passed as arguments GetBeansPtr(&bean1, &bean2, &bean3) => []*Bean{&bean1, &bean2, &bean3} ...*T is a variadic parameter that accepts a variable number of pointers to objects of type T. But the cost of using variadic parameters is that the compiler has to create a new slice and copy all the arguments into it. Note: Make sure to use this function only when the number of arguments is lower.

func GetDeReferencedSlice

func GetDeReferencedSlice[T any](ptrObjects []*T) []T

GetDeReferencedSlice converts an array of pointers to an array of values

func GetMapOf

func GetMapOf[K comparable, V comparable](sliceList []K, defaultValue V) map[K]V

GetMapOf returns a map with the specified sliceList as keys and defaultValue as values. for example, GetMapOf([1, 2, 3], "default") returns {1: "default", 2: "default", 3: "default"}

func GetMapValuesPtr

func GetMapValuesPtr[T any](valueMap map[string]*T) []*T

GetMapValuesPtr returns a slice of pointers to the values of the map passed as an argument

func GetReferencedSlice

func GetReferencedSlice[T any](objects []T) []*T

GetReferencedSlice converts an array of values to an array of pointers

func GetSliceOf

func GetSliceOf[T any](element T) []T

GetSliceOf returns a slice containing the specified element. for example, GetSliceOf(1) returns [1]

func GetUniqueElements

func GetUniqueElements[T comparable](sliceList []T) []T

GetUniqueElements returns a new slice containing only the unique elements of the specified sliceList. for example, GetUniqueElements([1, 2, 3, 2, 1]) returns [1, 2, 3]

func NewMapFromFuncExec

func NewMapFromFuncExec[T any, K comparable](input []T, transform func(inp T) K) map[K]T

NewMapFromFuncExec applies the given function to each element of the input slice And returns a new map with the transformed elements as keys and the original elements as values NewMapFromFuncExec([1, 2, 3], func(x int) string { return strconv.Itoa(x) }) => {"1": 1, "2": 2, "3": 3} NewMapFromFuncExec([{Name: "John", Age: 25}, {Name: "Doe", Age: 30}], func(x Person) string { return x.Name }) => {"John": {Name: "John", Age: 25}, "Doe": {Name: "Doe", Age: 30}} Note: The keys of the map should be unique, otherwise the last element with the same key will be stored in the map

func NewSliceFromFuncExec

func NewSliceFromFuncExec[T any, K any](input []T, transform func(inp T) K) []K

NewSliceFromFuncExec applies the given function to each element of the input slice And returns a new slice with the transformed elements NewSliceFromFuncExec([1, 2, 3], func(x int) int { return x * 2 }) => [2, 4, 6]

Types

This section is empty.

Jump to

Keyboard shortcuts

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