dict

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package dict contains Map functions

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clear

func Clear[K comparable, V any](items map[K]V)

Clear removes all map entries

func Copy

func Copy[K comparable, V any](items map[K]V) map[K]V

Copy creates a new map with copied entries

func CounterUpdate

func CounterUpdate[T comparable](oldCounter, newCounter Counter[T])

CounterUpdate updates the old counter with counts from new counter

func Filter

func Filter[K comparable, V any](items map[K]V, keep func(K, V) bool) map[K]V

Filter filters the map, only keeping entries that pass the keep function

func FromStruct

func FromStruct[V, T any](structRef *T) (map[string]V, error)

FromStruct creates a map[string]V from given struct pointer. Struct field values must all be of the same type.

func Get

func Get[T any](obj Object, key string) (T, bool)

Get retrieves the value from Object, then type coerces into type T

func GetList

func GetList[T any](obj Object, key string) []T

GetList retrieves the list value from Object, then type coerces into []T

func GetOrDefault

func GetOrDefault[K comparable, V any](items map[K]V, key K, defaultValue V) V

GetOrDefault gets the value associated with key if it exists, otherwise returns the default value

func GetRef

func GetRef[T any](obj Object, key string) *T

GetRef retrieves the ref value from Object, then type coerces into *T

func GroupByFunc

func GroupByFunc[K, B comparable, V, A any](items map[K]V, keyFn func(K) A, valueFn func(V) B) map[B][]A

GroupByFunc groups the map by values, using key and value transformers

func GroupByFuncList

func GroupByFuncList[K, B comparable, V, A any](items map[K][]V, keyFn func(K) A, valueFn func(V) B) map[B][]A

GroupByFuncList groups the map[K][]V by values, produces map[B][]A using key and value transformers

func GroupByValue

func GroupByValue[K, V comparable](items map[K]V) map[V][]K

GroupByValue groups the map by values

func GroupByValueList

func GroupByValueList[K, V comparable](items map[K][]V) map[V][]K

GroupByValueList groups the map[K][]V by values, produces map[V][]K

func HasKey

func HasKey[K comparable, V any](items map[K]V, key K) bool

HasKey checks if map has given key

func HasKeyFunc

func HasKeyFunc[K comparable, V any](items map[K]V, test func(K) bool) bool

HasKeyFunc checks if any map key passes the test function

func HasValue

func HasValue[K, V comparable](items map[K]V, value V) bool

HasValue checks if map has given value

func HasValueFunc

func HasValueFunc[K comparable, V any](items map[K]V, test func(V) bool) bool

HasValueFunc checks if any map value passes the test function

func IsEmpty

func IsEmpty[K comparable, V any](items map[K]V) bool

IsEmpty checks if map is empty

func Keys

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

Keys returns map keys, in arbitrary order

func KeysIter

func KeysIter[K comparable, V any](items map[K]V) iter.Seq[K]

KeysIter returns an iterator for map keys

func Len

func Len[K comparable, V any](items map[K]V) int

Len returns the map size

func LookupFunc

func LookupFunc[K comparable, V any](items map[K]V) func(K) (V, bool)

LookupFunc creates a converter function that returns associated value and exists flag for given key

func MustLookupFunc

func MustLookupFunc[K comparable, V any](items map[K]V) func(K) V

MustLookupFunc creates a converter function that returns associated value for given key, and panics if key is not found

func NoKey

func NoKey[K comparable, V any](items map[K]V, key K) bool

NoKey checks if map does not have given key

func NoKeyFunc

func NoKeyFunc[K comparable, V any](items map[K]V, test func(K) bool) bool

NoKeyFunc checks that no map key passes the test function

func NoValue

func NoValue[K, V comparable](items map[K]V, value V) bool

NoValue checks if map does not have given value

func NoValueFunc

func NoValueFunc[K comparable, V any](items map[K]V, test func(V) bool) bool

NoValueFunc checks that no Map value passes the test function

func NotEmpty

func NotEmpty[K comparable, V any](items map[K]V) bool

NotEmpty checks if map is not empty

func SetDefault

func SetDefault[K comparable, V any](items map[K]V, key K, defaultValue V)

SetDefault assigns default value to key, if key is not in Map

func SortValueLists

func SortValueLists[K comparable, V cmp.Ordered](items map[K][]V)

SortValueLists sorts the list of values in place, for each map key

func SortValueListsFunc

func SortValueListsFunc[K comparable, V any](items map[K][]V, sortFn func(V, V) int)

SortValueListsFunc sorts the list of values in place, using the sortFn, for each map key

func SortedKeys

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

SortedKeys returns the map keys in sorted order

func SortedKeysFunc

func SortedKeysFunc[K comparable, V any](items map[K]V, sortFn func(K, K) int) []K

SortedKeysFunc returns the map keys in sorted order, using the sortFn

func SortedUnzip

func SortedUnzip[K cmp.Ordered, V any](items map[K]V) ([]K, []V)

SortedUnzip returns the list of Map keys and values, where the keys are sorted and the value order corresponds to the key order

func SortedValues

func SortedValues[K comparable, V cmp.Ordered](items map[K]V) []V

SortedValues returns the map values in sorted order

func SortedValuesFunc

func SortedValuesFunc[K comparable, V any](items map[K]V, sortFn func(V, V) int) []V

SortedValuesFunc returns the map values in sorted order, using the sortFn

func Swap

func Swap[K, V comparable](items map[K]V) map[V]K

Swap swaps the map keys and values, converting map[K]V to map[V]K. This can lose data if values are not unique

func SwapList

func SwapList[K, V comparable](items map[K][]V) map[V]K

SwapList converts the map[K][]V to map[V]K. This can lose data if values are not unique

func ToStruct

func ToStruct[T any](obj Object) (*T, error)

ToStruct creates a struct reference from given Object

func Unzip

func Unzip[K comparable, V any](items map[K]V) ([]K, []V)

Unzip returns the list of Map keys and values, where the order of keys is the same as corresponding values

func Update

func Update[K comparable, V any](oldMap, newMap map[K]V)

Update adds the entries of new Map to current Map. If there are overlapping keys, new Map entries overwrite the old Map entries.

func UpdateCounter

func UpdateCounter[T comparable](counter Counter[T], items []T)

UpdateCounter updates the counter with incoming items

func UpdateCounterFunc

func UpdateCounterFunc[T any, K comparable](counter Counter[K], items []T, keyFn func(T) K)

UpdateCounterFunc updates the counter with incoming items, using the keyFn

func Values

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

Values returns map values, in arbitrary order

func ValuesIter

func ValuesIter[K comparable, V any](items map[K]V) iter.Seq[V]

ValuesIter returns an iterator for map values

func Zip

func Zip[K comparable, V any](keys []K, values []V) map[K]V

Zip creates a new map by zipping the keys and values

Types

type Bools

type Bools = map[string]bool

type Counter

type Counter[T comparable] = map[T]int

func MergeCounters

func MergeCounters[T comparable](counters ...Counter[T]) Counter[T]

MergeCounters merges the counts from given counters into one Counter

func NewCounterFor

func NewCounterFor[T comparable](items []T) Counter[T]

NewCounterFor creates a new counter, with each item initialized to count = 0

func NewCounterFunc

func NewCounterFunc[T any, K comparable](items []T, keyFn func(T) K) Counter[K]

NewCounterFunc creates a new counter, using the keys produced from keyFn

func TallyFunc

func TallyFunc[K, T comparable, V any](items map[K]V, valueFn func(V) T) Counter[T]

TallyFunc creates a tally of how many times a transformed value appears in the map, using value transformer

func TallyValues

func TallyValues[K, V comparable](items map[K]V, values []V) Counter[V]

TallyValues creates a tally of how many times each value appears in the map

type Entry

type Entry[K comparable, V any] struct {
	Key   K
	Value V
}

Entry represents a map Key-Value pair

func Entries

func Entries[K comparable, V any](items map[K]V) []Entry[K, V]

Entries returns map entries, in arbitrary order

func SortedEntries

func SortedEntries[K cmp.Ordered, V any](items map[K]V) []Entry[K, V]

SortedEntries returns the map entries in sorted key order

func SortedEntriesFunc

func SortedEntriesFunc[K comparable, V any](items map[K]V, sortFn func(Entry[K, V], Entry[K, V]) int) []Entry[K, V]

SortedEntriesFunc returns the map entries in sorted order, using the sortFn

func (Entry[K, V]) String

func (e Entry[K, V]) String() string

String returns the string representation of Entry

func (Entry[K, V]) Tuple

func (e Entry[K, V]) Tuple() (K, V)

Tuple returns the unpacked Key, Value of Entry

type Flags

type Flags[T comparable] = map[T]bool

func NewFlagsFor

func NewFlagsFor[T comparable](items []T, flag bool) Flags[T]

NewFlagsFor creates a new Flags map, with each item initialized to given flag

func NewFlagsFunc

func NewFlagsFunc[T any, K comparable](items []T, flag bool, keyFn func(T) K) Flags[K]

NewFlagsFunc creates a new Flags map, using the keyFn, with each key initialized to given flag

type Floats

type Floats = map[string]float64

type IntCounter

type IntCounter = map[int]int

type IntFlags

type IntFlags = map[int]bool

type Ints

type Ints = map[string]int

type Lookup

type Lookup[K comparable, V any] = map[K]V

type Object

type Object = map[string]any

func Pruned

func Pruned[T any](structRef *T, fieldNames ...string) (Object, error)

Pruned creates an Object from given struct pointer, but only keeps given fieldNames

func ToObject

func ToObject[T any](structRef *T) (Object, error)

ToObject creates an Object from given struct pointer

type StringCounter

type StringCounter = map[string]int

type StringFlags

type StringFlags = map[string]bool

type StringLists

type StringLists = map[string][]string

type Strings

type Strings = map[string]string

type UintCounter

type UintCounter = map[uint]int

type UintFlags

type UintFlags = map[uint]bool

type Uints

type Uints = map[string]uint

Jump to

Keyboard shortcuts

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