Documentation ¶
Overview ¶
Package snm provides convenience functions for slices and maps.
Index ¶
- func At[T any, I constraints.Integer](t []T, at []I) []T
- func Compare[T constraints.Ordered](a, b T) int
- func CompareReverse[T constraints.Ordered](a, b T) int
- func FilterMap[K comparable, V any](m map[K]V, keep func(k K, v V) bool) map[K]V
- func FilterSlice[S any](s []S, keep func(S) bool) []S
- func MapToMap[K comparable, V any, K2 comparable, V2 any](m map[K]V, f func(K, V) (K2, V2)) map[K2]V2
- func Slice[T any](n int, f func(int) T) []T
- func SliceToSlice[A any, B any](a []A, f func(A) B) []B
- func Sorted[T constraints.Ordered](s []T) []T
- func SortedFunc[T any](s []T, cmp func(T, T) int) []T
- type DefaultMap
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func At ¶
func At[T any, I constraints.Integer](t []T, at []I) []T
At returns the elements of t at the indexes in at.
func Compare ¶ added in v0.4.0
func Compare[T constraints.Ordered](a, b T) int
Compare is a generic comparator function for ordered types.
func CompareReverse ¶ added in v0.4.0
func CompareReverse[T constraints.Ordered](a, b T) int
CompareReverse is a generic comparator function for ordered types, that orders values from big to small.
func FilterMap ¶
func FilterMap[K comparable, V any](m map[K]V, keep func(k K, v V) bool) map[K]V
FilterMap returns a map containing only the elements for which keep returns true.
func FilterSlice ¶
FilterSlice returns a slice containing only the elements for which keep returns true.
func MapToMap ¶
func MapToMap[K comparable, V any, K2 comparable, V2 any]( m map[K]V, f func(K, V) (K2, V2)) map[K2]V2
MapToMap returns a map containing the results of applying f to the key-value pairs of m. f should return a new key-value pair for the new map. Keys that appear more than once will override each other.
func Slice ¶
Slice returns a new slice of size n whose values are the results of applying f on each index.
func SliceToSlice ¶
SliceToSlice returns a slice of the same length containing the results of applying f to the elements of s.
func SortedFunc ¶
SortedFunc sorts the input and returns it.
Types ¶
type DefaultMap ¶
type DefaultMap[K comparable, V any] struct { M map[K]V // Underlying map. Can be safely read from and written to. F func(K) V // Generator function. }
DefaultMap wraps a map with a function that generates values for missing keys.
func NewDefaultMap ¶
func NewDefaultMap[K comparable, V any](f func(K) V) DefaultMap[K, V]
NewDefaultMap returns an empty map with the given function as the missing value generator.
func (DefaultMap[K, V]) Get ¶
func (m DefaultMap[K, V]) Get(k K) V
Get returns the value associated with key k. If k is missing from the map, the generator function is called with k and the result becomes k's value.