Documentation
¶
Overview ¶
Package generics provides utility functions and data structures that make working with Go generics easier.
Index ¶
- func Clamp[T cmp.Ordered](low, high, val T) T
- func Coalesce[T comparable](values ...T) T
- func CoalesceByFunc[T any](isEqual func(T, T) bool, values ...T) T
- func CompareSlice[T comparable](s, target []T) bool
- func ConvertByJSON[T any](src any) (dest T, err error)
- func InBetween[T cmp.Ordered](low, high, val T) bool
- func InBetweenIncluding[T cmp.Ordered](low, high, val T) bool
- func InBetweenIncludingHigh[T cmp.Ordered](low, high, val T) bool
- func InBetweenIncludingLow[T cmp.Ordered](low, high, val T) bool
- func IsSubset[T comparable](a, b []T) bool
- func MapKeys[K comparable, V any](m map[K]V) (keys []K)
- func MapKeysAndValues[K comparable, V any](m map[K]V) (keys []K, values []V)
- func MapValues[K comparable, V any](m map[K]V) (values []V)
- func Max[T cmp.Ordered](values ...T) T
- func MaxByFunc[T any](less func(a, b T) bool, values ...T) T
- func Min[T cmp.Ordered](values ...T) T
- func MinByFunc[T any](less func(a, b T) bool, values ...T) T
- func SliceToBoolMap[K comparable, M ~[]K](s M) (res map[K]bool)
- func SliceToMap[K comparable, M ~[]K, T any](s M, defaultValue T) (res map[K]T)
- func Unique[K comparable](s []K) (u []K)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Clamp ¶
Clamp limits the value of val, if it's lower than low, it returns low, if val is higher that high, it returns high, otherwise returns val
func Coalesce ¶
func Coalesce[T comparable](values ...T) T
Coalesce, just like the sql function, returns the first non-zero value from values, if all values are zero, then the function returns the zero value
func CoalesceByFunc ¶
CoalesceByFunc, just like the sql function, returns the first non-zero value from values, it uses isEqual(T, T) to determine whether the values are equals to the zero value, if all values are zero, then the function returns the zero value
func CompareSlice ¶
func CompareSlice[T comparable](s, target []T) bool
CompareSlice determines whether 's' and 'target' are equals (same elements at the same position and same slice lengths)
func ConvertByJSON ¶
ConvertByJSON is a shortcut to the common use of the functions Marshal and Unmarshal from the json package
func InBetweenIncluding ¶
InBetweenIncluding checks if val belongs to [low, high]
func InBetweenIncludingHigh ¶
InBetweenIncludingHigh checks if val belongs to (low, high]
func InBetweenIncludingLow ¶
InBetweenIncludingLow checks if val belongs to [low, high)
func IsSubset ¶
func IsSubset[T comparable](a, b []T) bool
IsSubset returns whether a is subset of b (duplicates ignored)
func MapKeys ¶
func MapKeys[K comparable, V any](m map[K]V) (keys []K)
MapKeys returns the keys from m as slice
func MapKeysAndValues ¶
func MapKeysAndValues[K comparable, V any](m map[K]V) (keys []K, values []V)
MapKeysAndValues returns the keys and values of a map as separate slices
func MapValues ¶
func MapValues[K comparable, V any](m map[K]V) (values []V)
MapValues returns the values from m as slice
func MaxByFunc ¶
MaxByFunc returns the maximum element of values using the less function. less: returns whether a < b
func MinByFunc ¶
MinByFunc returns the minimum element of values using the less function. less: returns whether a < b
func SliceToBoolMap ¶
func SliceToBoolMap[K comparable, M ~[]K](s M) (res map[K]bool)
SliceToBoolMap transforms a slice of comparables to a boolean map with all elements set to 'true'
func SliceToMap ¶
func SliceToMap[K comparable, M ~[]K, T any](s M, defaultValue T) (res map[K]T)
SliceToMap transforms a slice of comparables to a custom map
Types ¶
This section is empty.