dict

package
v1.20.2 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package dict provides generic functions for working with maps.

This package offers type-safe operations on Go maps, including safe value retrieval, key/value extraction, filtering, and transformation operations.

Examples

// Safe value retrieval
m := map[string]int{"a": 1, "b": 2}
value := dict.Get(m, "a")
if value.IsSome() {
	fmt.Println(value.Unwrap()) // Output: 1
}

// Extract keys
keys := dict.Keys(m) // []string{"a", "b"}

// Filter map
filtered := dict.Filter(m, func(k string, v int) bool {
	return v > 1
}) // map[string]int{"b": 2}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Copy

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

Copy creates a copy of the map.

func Every

func Every[K comparable, V any](m map[K]V, fn func(k K, v V) bool) bool

Every tests whether all entries in the map pass the test implemented by the provided function. NOTE:

Calling this method on an empty map will return true for any condition!

func Filter

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

Filter creates a new map with all elements that pass the test implemented by the provided function.

func FilterMap

func FilterMap[K comparable, V any, K2 comparable, V2 any](m map[K]V, fn func(K, V) option.Option[DictEntry[K2, V2]]) map[K2]V2

FilterMap returns a filtered and mapped map of new entries.

func FilterMapKey

func FilterMapKey[K comparable, V any, K2 comparable](m map[K]V, fn func(K, V) option.Option[DictEntry[K2, V]]) map[K2]V

FilterMapKey returns a filtered and mapped map of new entries.

func FilterMapValue

func FilterMapValue[K comparable, V any, V2 any](m map[K]V, fn func(K, V) option.Option[DictEntry[K, V2]]) map[K]V2

FilterMapValue returns a filtered and mapped map of new entries.

func Find

func Find[K comparable, V any](m map[K]V, fn func(K, V) bool) option.Option[DictEntry[K, V]]

Find returns an entry in the provided map that satisfies the test function.

func Get added in v1.5.0

func Get[K comparable, V any](m map[K]V, k K) option.Option[V]

Get returns the option.Option[V] of the entry for the provided key.

func Keys

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

Keys returns the keys of map.

func Map

func Map[K comparable, V any, K2 comparable, V2 any](m map[K]V, mapping func(K, V) DictEntry[K2, V2]) map[K2]V2

Map creates a new map populated with the results of calling a provided function on every entry in the calling map.

func MapCurry

func MapCurry[K comparable, V any, K2 comparable, V2 any](m map[K]V, keyMapping func(K) K2) func(valueMapping func(V) V2) map[K2]V2

MapCurry creates a new map populated with the results of calling a provided function on every entry in the calling map.

func MapKey

func MapKey[K comparable, V any, K2 comparable](m map[K]V, mapping func(K, V) K2) map[K2]V

MapKey creates a new map populated with the results of calling a provided function on every entry in the calling map.

func MapKeyAlone

func MapKeyAlone[K comparable, V any, K2 comparable](m map[K]V, mapping func(K) K2) map[K2]V

MapKeyAlone creates a new map populated with the results of calling a provided function on every entry in the calling map.

func MapValue

func MapValue[K comparable, V any, V2 any](m map[K]V, mapping func(K, V) V2) map[K]V2

MapValue creates a new map populated with the results of calling a provided function on every entry in the calling map.

func MapValueAlone

func MapValueAlone[K comparable, V any, V2 any](m map[K]V, mapping func(V) V2) map[K]V2

MapValueAlone creates a new map populated with the results of calling a provided function on every entry in the calling map.

func Some

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

Some tests whether at least one entry in the map passes the test implemented by the provided function. NOTE:

Calling this method on an empty map returns false for any condition!

func Values

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

Values returns the values of map.

func Vec added in v1.5.0

func Vec[K comparable, V any, T any](m map[K]V, set func(K, V) T) []T

Vec generates an orderless slice through the set function.

Types

type DictEntry added in v1.20.0

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

DictEntry is a key-value entry of map.

func Entries

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

Entries returns the entries of map.

func (DictEntry[K, V]) Split added in v1.20.0

func (d DictEntry[K, V]) Split() (K, V)

Split splits the dictionary entry into its two components.

Jump to

Keyboard shortcuts

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