Documentation ¶
Overview ¶
Package memz provides various utilities for working with pointers, slices, and maps.
Index ¶
- func BatchSlice[T any](s []T, batchSize int) [][]T
- func ConcatSlices[T any](ss ...[]T) []T
- func FilterMap[K comparable, V any](m map[K]V, f func(k K, v V) bool) map[K]V
- func FilterSlice[T any](s []T, f func(t T) bool) []T
- func GetSortedMapKeys[K cmp.Ordered, V any](m map[K]V, less func(K, K) bool) []K
- func IsAnyNil(x any) bool
- func MapPtr[K comparable, V any](m map[K]V) map[K]*V
- func MapPtrZeroToNil[K comparable, V comparable](m map[K]V) map[K]*V
- func MapValNilToDef[K comparable, V any](m map[K]*V, d V) map[K]V
- func MapValNilToZero[K comparable, V any](m map[K]*V) map[K]V
- func Max[T cmp.Ordered](v1 T, vs ...T) T
- func MergeMaps[K comparable, V any](mm ...map[K]V) map[K]V
- func Min[T cmp.Ordered](v1 T, vs ...T) T
- func Ptr[T any](v T) *T
- func PtrIfTrue[T any](cond bool, v T) *T
- func PtrZeroToNil[T comparable](v T) *T
- func PtrZeroToNilIfTrue[T comparable](cond bool, v T) *T
- func SafeSliceIndexDef[T any](s []T, i int, d T) T
- func SafeSliceIndexPtr[T any](s []T, i int) *T
- func SafeSliceIndexZero[T any](s []T, i int) T
- func ShallowCopyMap[K comparable, V any](m map[K]V) map[K]V
- func ShallowCopySlice[T any](s []T) []T
- func SlicePtr[T any](s []T) []*T
- func SlicePtrZeroToNil[T comparable](s []T) []*T
- func SliceToStructMap[T comparable](s []T) map[T]struct{}
- func SliceValNilToDef[T any](s []*T, d T) []T
- func SliceValNilToZero[T any](s []*T) []T
- func Ternary[V any](cond bool, vT V, vF V) V
- func TransformMapValues[K comparable, V1 any, V2 any](m map[K]V1, f func(k K, v V1) V2) map[K]V2
- func TransformSlice[I any, O any](s []I, f func(i int, v I) O) []O
- func ValNilToDef[T any](v *T, d T) T
- func ValNilToZero[T any](v *T) T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BatchSlice ¶
BatchSlice splits a slice in batches.
func ConcatSlices ¶
func ConcatSlices[T any](ss ...[]T) []T
ConcatSlices returns a new slice built by appending all values from the given slices in order.
func FilterMap ¶
func FilterMap[K comparable, V any](m map[K]V, f func(k K, v V) bool) map[K]V
FilterMap copies a map including only (k, v) pairs for which the predicate returns true.
func FilterSlice ¶
FilterSlice makes a shallow copy of a slice including only elements for which the predicate returns true.
func GetSortedMapKeys ¶
GetSortedMapKeys returns a slice built by appending all keys in the map and sorting them.
func IsAnyNil ¶
IsAnyNil returns true if the given value is a "nil" of any type. Note that this method uses reflection.
func MapPtr ¶
func MapPtr[K comparable, V any](m map[K]V) map[K]*V
MapPtr returns a copy of the given map with each value passed through Ptr.
func MapPtrZeroToNil ¶
func MapPtrZeroToNil[K comparable, V comparable](m map[K]V) map[K]*V
MapPtrZeroToNil returns a copy of the given map with each value passed through PtrZeroToNil.
func MapValNilToDef ¶
func MapValNilToDef[K comparable, V any](m map[K]*V, d V) map[K]V
MapValNilToDef returns a copy of the given map with each value passed through ValNilToDef.
func MapValNilToZero ¶
func MapValNilToZero[K comparable, V any](m map[K]*V) map[K]V
MapValNilToZero returns a copy of the given map with each value passed through ValNilToZero.
func MergeMaps ¶
func MergeMaps[K comparable, V any](mm ...map[K]V) map[K]V
MergeMaps returns a new map built by setting all key/value pairs from the given maps in order.
func PtrZeroToNil ¶
func PtrZeroToNil[T comparable](v T) *T
PtrZeroToNil returns a pointer to the given value if different from the zero-value, nil otherwise.
func PtrZeroToNilIfTrue ¶
func PtrZeroToNilIfTrue[T comparable](cond bool, v T) *T
PtrZeroToNilIfTrue returns a pointer to the given value if different from the zero-value and cond is true, nil otherwise.
func SafeSliceIndexDef ¶
SafeSliceIndexDef indexes a slice, supports negative indexes (from end), and returns a default value instead of panic.
func SafeSliceIndexPtr ¶
SafeSliceIndexPtr indexes a slice, supports negative indexes (from end), and returns pointer to value instead of panic.
func SafeSliceIndexZero ¶
SafeSliceIndexZero indexes a slice, supports negative indexes (from end), and returns a zero-value instead of panic.
func ShallowCopyMap ¶
func ShallowCopyMap[K comparable, V any](m map[K]V) map[K]V
ShallowCopyMap makes a shallow copy of a map.
func ShallowCopySlice ¶
func ShallowCopySlice[T any](s []T) []T
ShallowCopySlice makes a shallow copy of a slice.
func SlicePtr ¶
func SlicePtr[T any](s []T) []*T
SlicePtr returns a copy of the given slice with each element passed through Ptr.
func SlicePtrZeroToNil ¶
func SlicePtrZeroToNil[T comparable](s []T) []*T
SlicePtrZeroToNil returns a copy of the given slice with each element passed through PtrZeroToNil.
func SliceToStructMap ¶ added in v0.3.0
func SliceToStructMap[T comparable](s []T) map[T]struct{}
SliceToStructMap converts a slice to struct map.
func SliceValNilToDef ¶
func SliceValNilToDef[T any](s []*T, d T) []T
SliceValNilToDef returns a copy of the given slice with each element passed through ValNilToDef.
func SliceValNilToZero ¶
func SliceValNilToZero[T any](s []*T) []T
SliceValNilToZero returns a copy of the given slice with each element passed through ValNilToZero.
func TransformMapValues ¶
func TransformMapValues[K comparable, V1 any, V2 any](m map[K]V1, f func(k K, v V1) V2) map[K]V2
TransformMapValues returns a new map built by passing all values through the given function, while the keys remain stable.
func TransformSlice ¶
TransformSlice returns a new slice built by passing all elements through the given function.
func ValNilToDef ¶
func ValNilToDef[T any](v *T, d T) T
ValNilToDef returns the value of the given pointer if found, the given default value otherwise.
func ValNilToZero ¶
func ValNilToZero[T any](v *T) T
ValNilToZero returns the value of the given pointer if found, a zero-value of the same type otherwise.
Types ¶
This section is empty.