snm

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2024 License: MIT Imports: 4 Imported by: 4

Documentation

Overview

Package snm provides convenience functions for slices and maps.

Index

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 Cast added in v0.5.0

func Cast[TO Number, FROM Number](s []FROM) []TO

Cast casts each element in the slice.

func Compare deprecated added in v0.4.0

func Compare[T constraints.Ordered](a, b T) int

Compare is a generic comparator function for ordered types.

Deprecated: use cmp.Compare instead.

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

func FilterSlice[S any](s []S, keep func(S) bool) []S

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 Slice

func Slice[T any](n int, f func(int) T) []T

Slice returns a new slice of size n whose values are the results of applying f on each index.

func SliceToSlice

func SliceToSlice[A any, B any](a []A, f func(A) B) []B

SliceToSlice returns a slice of the same length containing the results of applying f to the elements of s.

func Sorted

func Sorted[T constraints.Ordered](s []T) []T

Sorted sorts the input and returns it.

func SortedFunc

func SortedFunc[T any](s []T, cmp func(T, T) int) []T

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

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.

func (DefaultMap[K, V]) Set

func (m DefaultMap[K, V]) Set(k K, v V)

Set sets v as k's value.

type Number added in v0.5.0

type Number interface {
	constraints.Integer | constraints.Float
}

Number is an integer or a float.

Jump to

Keyboard shortcuts

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