mapx

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2026 License: Apache-2.0 Imports: 1 Imported by: 3

Documentation

Overview

Package mapx provides some extra map functions.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func All added in v0.4.0

func All[M ~map[K]V, K comparable, V any](m M) iter.Seq[Pair[K, V]]

All is the same as maps.All to return an iterator over key-value pairs from m, but uses Pair[K, V] to return iter.Seq instead of iter.Seq2.

Example
intm := map[int]int{1: 1, 2: 2, 3: 3}
pairs := slices.Collect(All(intm))
slices.SortFunc(pairs, func(a, b Pair[int, int]) int { return a.Key - b.Key })

for _, p := range pairs {
	fmt.Printf("%d -> %d\n", p.Key, p.Value)
}

var keys []int
All(intm)(func(p Pair[int, int]) bool {
	keys = append(keys, p.Key)
	return false // Only append one key randomly
})
fmt.Println("KeyNum:", len(keys))
Output:

1 -> 1
2 -> 2
3 -> 3
KeyNum: 1

func Convert

func Convert[M ~map[K1]V1, K1, K2 comparable, V1, V2 any](maps M, convert func(K1, V1) (K2, V2)) map[K2]V2

Convert converts the map from map[K1]V1 to map[K2]V2.

DEPRECATED. Please use To instead.

Example
type Maps map[string]int

var nilmap1 Maps
nilmap2 := Convert(nilmap1, _convert)
if nilmap2 == nil {
	fmt.Println("nil")
} else {
	fmt.Printf("%v\n", nilmap2)
}

int64map1 := Convert(Maps{"a": 1, "b": 2}, _convert)
int64map2 := Convert(map[string]int{"a": 3, "b": 4}, _convert)

fmt.Printf("%T\n", int64map1)
fmt.Printf("%T\n", int64map2)
fmt.Printf("%s=%v\n", "a", int64map1["a"])
fmt.Printf("%s=%v\n", "b", int64map1["b"])
fmt.Printf("%s=%v\n", "a", int64map2["a"])
fmt.Printf("%s=%v\n", "b", int64map2["b"])
Output:

nil
map[string]int64
map[string]int64
a=1
b=2
a=3
b=4

func Empty added in v0.11.0

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

Empty returns itself if vs is not nil, otherwise returns an empty map.

Example
var m map[string]int
fmt.Println(m == nil, len(m))

m = Empty(m)
fmt.Println(m == nil, len(m))

m = map[string]int{"a": 1}
fmt.Println(m)
Output:

true 0
false 0
map[a:1]

func Filter added in v0.9.0

func Filter[M ~map[K1]V1, K1, K2 comparable, V1, V2 any](m M, filter func(K1, V1) (K2, V2, bool)) map[K2]V2

Filter filters the elements of the map m and converts them.

Example
type Maps map[string]int

var nilmap1 Maps
nilmap2 := Filter(nilmap1, _filter)
if nilmap2 == nil {
	fmt.Println("nil")
} else {
	fmt.Printf("%v\n", nilmap2)
}

int64map1 := Filter(Maps{"a": 1, "b": 2}, _filter)
int64map2 := Filter(map[string]int{"a": 3, "b": 4}, _filter)

fmt.Printf("%T, %v\n", int64map1, int64map1)
fmt.Printf("%T, %v\n", int64map2, int64map2)
Output:

nil
map[string]int64, map[b:2]
map[string]int64, map[b:4]

func Keys

func Keys[M ~map[K]V, K comparable, V any](maps M) []K

Keys returns all the keys of the map.

Example
intmap := map[int]int{1: 11, 2: 22}
ints := Keys(intmap)
sort.Ints(ints)
fmt.Println(ints)

strmap := map[string]string{"a": "aa", "b": "bb"}
strs := Keys(strmap)
sort.Strings(strs)
fmt.Println(strs)
Output:

[1 2]
[a b]

func KeysFunc

func KeysFunc[M ~map[K]V, T any, K comparable, V any](maps M, convert func(K) T) []T

KeysFunc returns all the keys of the map by the conversion function.

Example
type Key struct {
	K string
	V int32
}
maps := map[Key]bool{
	{K: "a", V: 1}: true,
	{K: "b", V: 2}: true,
	{K: "c", V: 3}: true,
}

keys := KeysFunc(maps, func(k Key) string { return k.K })
slices.Sort(keys)
fmt.Println(keys)
Output:

[a b c]

func To added in v0.9.0

func To[M ~map[K1]V1, K1, K2 comparable, V1, V2 any](maps M, convert func(K1, V1) (K2, V2)) map[K2]V2

To converts the map from map[K1]V1 to map[K2]V2.

func Values

func Values[M ~map[K]V, K comparable, V any](maps M) []V

Values returns all the values of the map.

Example
intmap := map[int]int{1: 11, 2: 22}
ints := Values(intmap)
sort.Ints(ints)
fmt.Println(ints)

strmap := map[string]string{"a": "aa", "b": "bb"}
strs := Values(strmap)
sort.Strings(strs)
fmt.Println(strs)
Output:

[11 22]
[aa bb]

func ValuesFunc

func ValuesFunc[M ~map[K]V, T any, K comparable, V any](maps M, convert func(V) T) []T

Values returns all the values of the map by the conversion function.

Example
type Value struct {
	V int
}
maps := map[string]Value{
	"a": {V: 1},
	"b": {V: 2},
	"c": {V: 3},
}

values := ValuesFunc(maps, func(v Value) int { return v.V })
slices.Sort(values)
fmt.Println(values)
Output:

[1 2 3]

Types

type Pair added in v0.4.0

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

Pair represents a key-value pair of map.

Jump to

Keyboard shortcuts

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