mapx

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package mapx contains convenience utilities for working with maps. Some functionality here is expected to be subsumed by the standard library at some point.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clone

func Clone[K comparable, V any](m map[K]V) map[K]V

Clone makes a copy of the map (with value copy of keys and values). TODO(jhhurwitz): 09/16/24 Remove when we upgrade to go 1.23 (https://github.com/golang/go/issues/69110)

func Contains

func Contains[K comparable, V any](m map[K]V, k K) bool

Contains returns true if the given key is in the map.

func ContainsFunc

func ContainsFunc[K comparable, V any](m map[K]V, f func(k K, v V) bool) bool

ContainsFunc returns true if at least one key-value pair satisfies f(k, v).

func FilterKeys

func FilterKeys[K comparable, V any](m map[K]V, fn func(K) bool) map[K]V

FilterKeys returns elements with Keys matching the filter function.

func Flatten

func Flatten[K comparable, V any](m map[K][]V) []V

Flatten extracts all value elements of a multi-map to a single slice.

func FlattenMap

func FlattenMap[K comparable, V, T any](m map[K][]V, f func(v V) T) []T

FlattenMap extracts all value elements of a multi-map to a single slice with a transformation.

func GetOnly

func GetOnly[K comparable, V any](m map[K]V) (K, V, bool)

GetOnly returns the only key-value pair in the map. When used with a map that has more than one element, returns an arbitrary pair and true. When used with an empty map, returns the zero values and false.

func Keys

func Keys[K comparable, V any](m map[K]V) []K

Keys extracts all keys to a slice.

func Map

func Map[K, K1 comparable, V, V1 any](m map[K]V, fn func(K, V) (K1, V1)) map[K1]V1

Map extracts all transformed keys and values to a map.

func MapIf

func MapIf[K, K1 comparable, V, V1 any](m map[K]V, fn func(K, V) (K1, V1, bool)) map[K1]V1

MapIf extracts all transformed keys and values to a map, if they satisfy a given predicate

func MapNew

func MapNew[K comparable, V any, T any](values []T, fn func(T) (K, V)) map[K]V

MapNew returns a map from transformed values.

func MapToSlice

func MapToSlice[K comparable, V, T any](m map[K]V, fn func(k K, v V) T) []T

MapToSlice extracts all transformed entries to a slice.

func MapValues

func MapValues[K comparable, V, T any](m map[K]V, fn func(V) T) []T

MapValues extracts all transformed values to a slice.

func MapValuesIf

func MapValuesIf[K comparable, V, T any](m map[K]V, fn func(V) (T, bool)) []T

MapValuesIf extracts selected transformed values to a slice.

func Merge

func Merge[K comparable, V any](maps ...map[K]V) map[K]V

Merge combines multiple maps into a single map. Values with identical keys are overridden by the last one.

func New

func New[K comparable, V any](values []V, keyOf func(V) K) map[K]V

New returns a map from intrinsically keyed values.

func SortedKeys

func SortedKeys[K cmp.Ordered, V any](m map[K]V) []K

SortedKeys extracts all keys to a slice and sorts them.

func TryMap

func TryMap[K, K1 comparable, V, V1 any](m map[K]V, fn func(K, V) (K1, V1, error)) (map[K1]V1, error)

TryMap extracts all transformed keys and values to a map.

func TryMapIf

func TryMapIf[K, K1 comparable, V, V1 any](m map[K]V, fn func(K, V) (K1, V1, bool, error)) (map[K1]V1, error)

TryMapIf extracts all transformed keys and values to a map, if they satisfy a given predicate

func TryMapToSlice

func TryMapToSlice[K comparable, V, T any](m map[K]V, fn func(k K, v V) (T, error)) ([]T, error)

TryMapToSlice extracts all transformed entries to a slice.

func Values

func Values[K comparable, V any](m map[K]V) []V

Values extracts all values to a slice.

func ValuesIf

func ValuesIf[K comparable, V any](m map[K]V, fn func(V) bool) []V

ValuesIf extracts all values to a slice, if they satisfy the given predicate.

Types

type Internalizer

type Internalizer[V any] struct {
	// contains filtered or unexported fields
}

Internalizer offers shared, unique representations of immutable values, usually reference types. It requires the caller to pick a (deterministic and complete) serialization function and not mutate internalized values.

func NewInternalizer

func NewInternalizer[V any](fn SerializeFn[V]) *Internalizer[V]

func (*Internalizer[V]) Internalize

func (i *Internalizer[V]) Internalize(v V) V

Internalize returns a shared equivalent value of v.

func (*Internalizer[V]) Reset

func (i *Internalizer[V]) Reset()

func (*Internalizer[V]) String

func (i *Internalizer[V]) String() string

type SerializeFn

type SerializeFn[V any] func(V) ([]byte, error)

Jump to

Keyboard shortcuts

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