memz

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2024 License: MIT Imports: 3 Imported by: 10

Documentation

Overview

Package memz provides various utilities for working with pointers, slices, and maps.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BatchSlice

func BatchSlice[T any](s []T, batchSize int) [][]T

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

func FilterSlice[T any](s []T, f func(t T) bool) []T

FilterSlice makes a shallow copy of a slice including only elements for which the predicate returns true.

func GetSortedMapKeys

func GetSortedMapKeys[K cmp.Ordered, V any](m map[K]V, less func(K, K) bool) []K

GetSortedMapKeys returns a slice built by appending all keys in the map and sorting them.

func IsAnyNil

func IsAnyNil(x any) bool

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 Max

func Max[T cmp.Ordered](v1 T, vs ...T) T

Max returns the highest value.

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 Min

func Min[T cmp.Ordered](v1 T, vs ...T) T

Min returns the lowest value.

func Ptr

func Ptr[T any](v T) *T

Ptr returns a pointer to the given value.

func PtrIfTrue

func PtrIfTrue[T any](cond bool, v T) *T

PtrIfTrue returns a pointer to the given value of cond is true, nil otherwise.

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

func SafeSliceIndexDef[T any](s []T, i int, d T) T

SafeSliceIndexDef indexes a slice, supports negative indexes (from end), and returns a default value instead of panic.

func SafeSliceIndexPtr

func SafeSliceIndexPtr[T any](s []T, i int) *T

SafeSliceIndexPtr indexes a slice, supports negative indexes (from end), and returns pointer to value instead of panic.

func SafeSliceIndexZero

func SafeSliceIndexZero[T any](s []T, i int) T

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 Ternary added in v0.2.0

func Ternary[V any](cond bool, vT V, vF V) V

Ternary returns "vT" if "cond" is true, "vF" otherwise.

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

func TransformSlice[I any, O any](s []I, f func(i int, v I) O) []O

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.

Jump to

Keyboard shortcuts

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