gmap

package
v0.3.7 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2025 License: Apache-2.0 Imports: 0 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clone

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

Clone returns a shallow copy of map. If the given map is nil, nil is returned.

🚀 EXAMPLE:

Clone(map[int]int{1: 1, 2: 2}) ⏩ map[int]int{1: 1, 2: 2}
Clone(map[int]int{})           ⏩ map[int]int{}
Clone[int, int](nil)           ⏩ nil

💡 HINT: Both keys and values are copied using assignment (=), so this is a shallow clone. 💡 AKA: Copy

func Concat

func Concat[K comparable, V any](ms ...map[K]V) map[K]V

Concat returns the unions of maps as a new map.

💡 NOTE:

  • Once the key conflicts, the newer value always replace the older one ([DiscardOld]),
  • If the result is an empty set, always return an empty map instead of nil

🚀 EXAMPLE:

m := map[int]int{1: 1, 2: 2}
Concat(m, nil)             ⏩ map[int]int{1: 1, 2: 2}
Concat(m, map[int]{3: 3})  ⏩ map[int]int{1: 1, 2: 2, 3: 3}
Concat(m, map[int]{2: -1}) ⏩ map[int]int{1: 1, 2: -1} // "2:2" is replaced by the newer "2:-1"

💡 AKA: Merge, Union, Combine

func Map

func Map[K1, K2 comparable, V1, V2 any](m map[K1]V1, f func(K1, V1) (K2, V2)) map[K2]V2

Map applies function f to each key and value of map m. Results of f are returned as a new map.

🚀 EXAMPLE:

f := func(k, v int) (string, string) { return strconv.Itoa(k), strconv.Itoa(v) }
Map(map[int]int{1: 1}, f) ⏩ map[string]string{"1": "1"}
Map(map[int]int{}, f)     ⏩ map[string]string{}

func Values

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

Values returns the values of the map m.

🚀 EXAMPLE:

m := map[int]string{1: "1", 2: "2", 3: "3", 4: "4"}
Values(m) ⏩ []string{"1", "4", "2", "3"} //⚠️INDETERMINATE ORDER⚠️

⚠️ WARNING: The keys values be in an indeterminate order,

Types

This section is empty.

Jump to

Keyboard shortcuts

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