Documentation ¶
Index ¶
- func ConstrainMap[K comparable, V any](src map[K]any) (map[K]V, error)
- func FilterMap[KS, KD comparable, VS, VD any](src map[KS]VS, f func(KS, VS) (k KD, v VD, ok bool)) map[KD]VD
- func FromKeyVals[K comparable, V any](a []KeyVal[K, V]) map[K]V
- func Keys[K comparable, V any](m map[K]V) []K
- func MapFromItems[K comparable, V any](k []K, v []V) (map[K]V, error)
- func MapItems[K comparable, V any](m map[K]V) (keys []K, vals []V)
- func MapResult[K comparable, V0, V1 any](src map[K]V0, f func(V0) (V1, error)) (map[K]V1, error)
- func MapVals[K comparable, V any](m map[K]V) []V
- func MustMapFromItems[K comparable, V any](k []K, v []V) map[K]V
- func Slice[T, B any](in []T, f func(T) B) []B
- func SliceResult[T, B any](in []T, f func(T) (B, error)) (out []B, err error)
- func SortedKeys[K constraints.Ordered, V any](m map[K]V) []K
- func SortedMapItems[K constraints.Ordered, V any](m map[K]V) (keys []K, vals []V)
- func WidenMap[K comparable, V any](src map[K]V) map[K]any
- type KeyVal
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConstrainMap ¶
func ConstrainMap[K comparable, V any](src map[K]any) (map[K]V, error)
ConstrainMap attempts to convert a map[K]any to a map with more concrete values. It returns an error if it an encounters an element not of type T.
func FilterMap ¶
func FilterMap[KS, KD comparable, VS, VD any]( src map[KS]VS, f func(KS, VS) (k KD, v VD, ok bool), ) map[KD]VD
FilterMap maps a map[KS]VS to a map[KS]VS by applying f and dropping the elements where f(ks, vs) returns _, _, false. eg:
toPositiveInt := func(s string) (int, bool) { n, err := strconv.Atoi(s) if err != nil || n <= 0 { return n, false } return n, true } FilterMap[map[string]{v:}]
func FromKeyVals ¶
func FromKeyVals[K comparable, V any](a []KeyVal[K, V]) map[K]V
FromKeyVals creates a map from the slice a by setting m[kv.K] = kv.V for each item in a. This expects unique, non-NaN keys. The behavior of duplicate keys or NaN is not specified.
func Keys ¶
func Keys[K comparable, V any](m map[K]V) []K
Keys returns a slice of the map's keys in an arbitrary order.
func MapFromItems ¶
func MapFromItems[K comparable, V any](k []K, v []V) (map[K]V, error)
FromItems creates a map from equal-length slices k and v. Constraints:
- The slices must be of equal length
- the items in k must be distinct.
Violation of either returns an error.
func MapItems ¶
func MapItems[K comparable, V any](m map[K]V) (keys []K, vals []V)
Items returns the keys and values of the map m. keys[i] always corresponds to values[i], but otherwise they're in arbitrary oder. Use MapAsKeyVals if you'd rather have a slice of structs.
func MapResult ¶
func MapResult[K comparable, V0, V1 any](src map[K]V0, f func(V0) (V1, error)) (map[K]V1, error)
MapResult attempts to convert a map[K0][V0] to a map[K1][V1], returning early if it encounters an error.
func MapVals ¶
func MapVals[K comparable, V any](m map[K]V) []V
MapVals returns a slice of the map's values in an arbitrary order.
func MustMapFromItems ¶
func MustMapFromItems[K comparable, V any](k []K, v []V) map[K]V
MustMapFromItems creates a map from equal-length slices k and v. Constraints:
- The slices must be of equal length
- the items in k must be distinct.
Violation of either panics.
func Slice ¶
func Slice[T, B any](in []T, f func(T) B) []B
SliceResult attempts to convert a slice of []T to a slice of []B. This is roughly equivalent to the "map" function in Python, Rust, etc. In general, it's better to just use a for loop for clarity: Go doesn't really support the functional style.
func SliceResult ¶
SliceResult attempts to convert a slice of []T to a slice of []B, returning when it encounters it's first error. This is equivalent to the "map" function in Python, Rust, etc. In general, it's better to just use a for loop for clarity: Go doesn't really support the functional style.
func SortedKeys ¶
func SortedKeys[K constraints.Ordered, V any](m map[K]V) []K
Keys returns a slice of the map's keys in a sorted order.
func SortedMapItems ¶
func SortedMapItems[K constraints.Ordered, V any](m map[K]V) (keys []K, vals []V)
SortedMapItems turns a map[K][V] into a pair of slices where keys[i] always corresponds to values[i], sorted by the keys. That is, if i < j, keys[i] <= keys[j].
func WidenMap ¶
func WidenMap[K comparable, V any](src map[K]V) map[K]any
WidenMap converts a map[K][V] to a map[K] any.
Types ¶
type KeyVal ¶
type KeyVal[K, V any] struct { K K V V }
KeyVal represents a key:value pair. Useful as a compact and stack-alloc-able representation of a map entry.
func MapAsKeyVals ¶
func MapAsKeyVals[K comparable, V any](m map[K]V) []KeyVal[K, V]
AsKeyVals creates a slice of []KeyVal{K,V} in an arbitrary order. See MapItems if you'd rather have a pair of slices.
func SortedMapKeyVals ¶
func SortedMapKeyVals[K constraints.Ordered, V any](m map[K]V) []KeyVal[K, V]
SortedMapKeyVals creates a slice of []KeyVal{K,V} sorted by the keys.