Documentation ¶
Overview ¶
util contains small standalone utility functions. This should have no dependencies on other server packages.
Index ¶
- func CloneMapNonNil[M ~map[K]V, K comparable, V any](m M) M
- func Coalesce[T comparable](vals ...T) T
- func FilterSlice[T any](in []T, predicate func(T) bool) []T
- func FoldSlice[T any, A any](in []T, initializer A, reducer func(A, T) A) A
- func InverseMap[M ~map[K]V, K, V comparable](m M) map[V]K
- func MapConcurrent[IN any, OUT any](input []IN, mapper func(IN) (OUT, error)) ([]OUT, error)
- func MapSlice[T, S any](xs []T, f func(T) S) []S
- func MaxTime(a, b time.Time) time.Time
- func MinTime(a, b time.Time) time.Time
- func RepeatSlice[T any](xs []T, n int) []T
- func SliceHead[S ~[]E, E any](s S, n int) S
- func SliceTail[S ~[]E, E any](s S, n int) S
- func SortSlice[S ~[]E, E constraints.Ordered](slice S)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CloneMapNonNil ¶ added in v1.18.4
func CloneMapNonNil[M ~map[K]V, K comparable, V any](m M) M
CloneMapNonNil is like maps.Clone except it can't return nil, it will return an empty map instead.
func Coalesce ¶ added in v1.23.0
func Coalesce[T comparable](vals ...T) T
Coalesce returns the first non-zero value of its arguments, or the zero value for the type if all are zero.
func FilterSlice ¶ added in v1.21.0
FilterSlice iterates over elements of a slice, returning a new slice of all elements predicate returns true for.
func FoldSlice ¶ added in v1.23.0
FoldSlice folds left a slice using given reducer function and initial value.
func InverseMap ¶ added in v1.20.0
func InverseMap[M ~map[K]V, K, V comparable](m M) map[V]K
InverseMap creates the inverse map, ie., for a key-value map, it builds the value-key map.
func MapConcurrent ¶ added in v1.21.0
MapConcurrent concurrently maps a function over input and fails fast on error.
func MapSlice ¶ added in v1.23.0
func MapSlice[T, S any](xs []T, f func(T) S) []S
MapSlice given slice xs []T and f(T) S produces slice []S by applying f to every element of xs
func RepeatSlice ¶ added in v1.23.0
RepeatSlice given slice and a number (n) produces a new slice containing original slice n times if n is non-positive will produce nil
func SortSlice ¶
func SortSlice[S ~[]E, E constraints.Ordered](slice S)
SortSlice sorts the given slice of an ordered type. Sort is not guaranteed to be stable.
Types ¶
This section is empty.