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 Cast[TO gnum.Number, FROM gnum.Number](s []FROM) []TO
- func ClearShrink[T any](a []T) []T
- 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 Shuffle[S ~[]T, T any](s S)
- 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
- func SortedKeys[K comparable, V constraints.Ordered](m map[K]V) []K
- func SortedKeysFunc[K comparable, V constraints.Ordered](m map[K]V, cmp func(V, V) int) []K
- func TightClone[T any](a []T) []T
- type CapMap
- type DefaultMap
- type Enumerator
- type Queue
Examples ¶
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 ClearShrink ¶ added in v1.1.0
func ClearShrink[T any](a []T) []T
ClearShrink returns a with length reset to 0, possibly reallocating it with a smaller capacity.
This is helpful for code that reuses the same buffer and clears it between uses, for making sure the buffer doesn't stay unnecessarily large after one large input.
func CompareReverse ¶ added in v0.4.0
func CompareReverse[T constraints.Ordered](a, b T) int
CompareReverse orders values from big to small. Should be generally used as a parameter, not called.
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 new map containing only the elements for which keep returns true.
func FilterSlice ¶
FilterSlice returns a new 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 Shuffle ¶ added in v1.1.0
func Shuffle[S ~[]T, T any](s S)
Shuffle reorders the given slice randomly, with a uniform distribution.
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.
func SortedKeys ¶ added in v0.5.0
func SortedKeys[K comparable, V constraints.Ordered]( m map[K]V) []K
SortedKeys sorts a map's keys according to their values' natural order.
Example ¶
ages := map[string]int{
"Alice": 30,
"Bob": 20,
"Charlie": 25,
}
for _, name := range SortedKeys(ages) {
fmt.Printf("%s: %d\n", name, ages[name])
}
Output: Bob: 20 Charlie: 25 Alice: 30
func SortedKeysFunc ¶ added in v0.5.0
func SortedKeysFunc[K comparable, V constraints.Ordered]( m map[K]V, cmp func(V, V) int) []K
SortedKeysFunc sorts a map's keys by comparing their values.
Example (Reverse) ¶
ages := map[string]int{
"Alice": 30,
"Bob": 20,
"Charlie": 25,
}
// Sort by reverse natural order.
for _, name := range SortedKeysFunc(ages, CompareReverse) {
fmt.Printf("%s: %d\n", name, ages[name])
}
Output: Alice: 30 Charlie: 25 Bob: 20
func TightClone ¶ added in v1.1.0
func TightClone[T any](a []T) []T
TightClone returns a shallow clone of a, with a capacity equal to its length.
Types ¶
type CapMap ¶ added in v0.6.0
type CapMap[K comparable, V any] struct { // contains filtered or unexported fields }
CapMap is a wrapper over a regular map, with a Clear function that possibly reallocates the map.
This is helpful for code that reuses the same map and clears it between uses, for making sure the map doesn't stay unnecessarily large after one large input.
Example ¶
data := [][]string{
{"a", "b", "c", "a", "b", "b"},
// ...
}
counter := NewCapMap[string, int]()
for _, x := range data {
m := counter.Map()
countValues(x, m)
// Do something with m.
j, _ := json.Marshal(m)
fmt.Println(string(j))
counter.Clear()
}
Output: {"a":2,"b":3,"c":1}
func NewCapMap ¶ added in v0.6.0
func NewCapMap[K comparable, V any]() *CapMap[K, V]
NewCapMap returns a new empty CapMap.
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.
type Enumerator ¶ added in v0.6.0
type Enumerator[T comparable] map[T]int
Enumerator enumerates values by their order of appearance.
func (Enumerator[T]) Elements ¶ added in v0.6.0
func (e Enumerator[T]) Elements() []T
Elements returns the enumerated elements, by order of appearance.
func (Enumerator[T]) IndexOf ¶ added in v0.6.0
func (e Enumerator[T]) IndexOf(t T) int
IndexOf returns the index of t, possibly allocating a new one.
Equal values always have the same index, while different values always have different indexes. Indexes are sequential.
type Queue ¶ added in v0.6.0
type Queue[T any] struct { // contains filtered or unexported fields }
Queue is a memory-efficient FIFO container.
func (*Queue[T]) Dequeue ¶ added in v0.6.0
func (q *Queue[T]) Dequeue() T
Dequeue removes and returns the next element in the queue. Panics if the queue is empty.
func (*Queue[T]) Enqueue ¶ added in v0.6.0
func (q *Queue[T]) Enqueue(x T)
Enqueue inserts an element to the queue.