maputil

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2023 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package maputil includes some functions to manipulate map.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Filter

func Filter[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V

Filter iterates over map, return a new map contains all key and value pairs pass the predicate function. Play: https://go.dev/play/p/fSvF3wxuNG7

Example
m := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
	"d": 4,
	"e": 5,
}

isEven := func(_ string, value int) bool {
	return value%2 == 0
}

result := Filter(m, isEven)

fmt.Println(result)
Output:

map[b:2 d:4]

func ForEach

func ForEach[K comparable, V any](m map[K]V, iteratee func(key K, value V))

ForEach executes iteratee funcation for every key and value pair in map. Play: https://go.dev/play/p/OaThj6iNVXK

Example
m := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
	"d": 4,
}

var sum int
ForEach(m, func(_ string, value int) {
	sum += value
})

fmt.Println(sum)
Output:

10

func Intersect

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

Intersect iterates over maps, return a new map of key and value pairs in all given maps. Play: https://go.dev/play/p/Zld0oj3sjcC

Example
m1 := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
}

m2 := map[string]int{
	"a": 1,
	"b": 2,
	"c": 6,
	"d": 7,
}

m3 := map[string]int{
	"a": 1,
	"b": 9,
	"e": 9,
}

result1 := Intersect(m1)
result2 := Intersect(m1, m2)
result3 := Intersect(m1, m2, m3)

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
Output:

map[a:1 b:2 c:3]
map[a:1 b:2]
map[a:1]

func IsDisjoint

func IsDisjoint[K comparable, V any](mapA, mapB map[K]V) bool

IsDisjoint two map are disjoint if they have no keys in common. Play: https://go.dev/play/p/N9qgYg_Ho6f

Example
m1 := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
}

m2 := map[string]int{
	"d": 22,
}

m3 := map[string]int{
	"a": 22,
}

result1 := IsDisjoint(m1, m2)
result2 := IsDisjoint(m1, m3)

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func Keys

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

Keys returns a slice of the map's keys. Play: https://go.dev/play/p/xNB5bTb97Wd

Example
m := map[int]string{
	1: "a",
	2: "a",
	3: "b",
	4: "c",
	5: "d",
}

keys := Keys(m)
sort.Ints(keys)

fmt.Println(keys)
Output:

[1 2 3 4 5]

func Merge

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

Merge maps, next key will overwrite previous key. Play: https://go.dev/play/p/H95LENF1uB-

Example
m1 := map[int]string{
	1: "a",
	2: "b",
}
m2 := map[int]string{
	1: "c",
	3: "d",
}

result := Merge(m1, m2)

fmt.Println(result)
Output:

map[1:c 2:b 3:d]

func Minus

func Minus[K comparable, V any](mapA, mapB map[K]V) map[K]V

Minus creates a map of whose key in mapA but not in mapB. Play: https://go.dev/play/p/3u5U9K7YZ9m

Example
m1 := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
}

m2 := map[string]int{
	"a": 11,
	"b": 22,
	"d": 33,
}

result := Minus(m1, m2)

fmt.Println(result)
Output:

map[c:3]

func Values

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

Values returns a slice of the map's values. Play: https://go.dev/play/p/CBKdUc5FTW6

Example
m := map[int]string{
	1: "a",
	2: "a",
	3: "b",
	4: "c",
	5: "d",
}

values := Values(m)
sort.Strings(values)

fmt.Println(values)
Output:

[a a b c d]

Types

This section is empty.

Jump to

Keyboard shortcuts

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