maps

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

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

The All function returns iter.Seq2 with map elemts. Right way to use.

iter = maps.All(mapCollection)

func AllFunc added in v1.0.0

func AllFunc[M ~map[K]V, K comparable, V any](m M, cmd func(K, V) bool) bool

The AllFunc function returns true if cmd function return true for all elements. If collection is empty, true returns. So you may need to check if collection is empty. Right way to use

if maps.AllFunc(map[int]struct{}{}, func(key int, val struct{}) bool { return key&1 == 0 } ) { ...

func AnyFunc

func AnyFunc[M ~map[K]V, K comparable, V any](m M, cmd func(K, V) bool) bool

The AnyFunc function returns true if cmd function return true for one or more. If collection is empty, false returns. So you may need to check collection on empty. Right way to use

if maps.AnyFunc(map[int]struct{}{}, func(key int, val struct{}) bool { return key&1 == 0 } ) { ...

func Append

func Append[M ~map[K]V, K comparable, V any](first, second M)

The Append function append values from second map to first. Source map changing

func BucketingJoin

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

func DeleteFunc

func DeleteFunc[M ~map[K]V, K comparable, V any](m map[K]V, filter func(K, V) bool)

The DeleteFunc function select key of a map that unsatisfy to filter function. !It updates source map.

maps.DeleteFunc(m, filterFunc)

func Filter

func Filter[M ~map[K]V, K comparable, V any](m M, filter func(K, V) bool) M

The Filter function select elements of a map that satisfy to filter function. It creates new map with same size and append only satisfing elements. Filter returns the updated map. It is therefore necessary to store the result of filtering, often in the variable holding the map itself:

m = maps.Filter(m, filterFunc)

func First added in v1.0.0

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

The First function returns first element of map. If collection is empty, function panic. So you may need to check if collection is empty.

func FirstFunc added in v1.0.0

func FirstFunc[M ~map[K]V, K comparable, V any](m M, cmd func(K, V) bool) (K, V)

The FirstFunc function returns first element of map satisfying filter (cmd). If there is no satisfying elements in collection, function panic You should use FirstOrErrFunc function to avoid panics

func FirstOrErrFunc added in v1.0.0

func FirstOrErrFunc[M ~map[K]V, K comparable, V any](m M, cmd func(K, V) bool) (k K, v V, err error)

The FirstFunc function returns first element of map satisfying filter (cmd). If there is no satisfying elements in collection, function panic

func ForEach

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

The ForEach function exec any func for each element of map. !!!Pay attention, if you want to change map non ref values you should use TransformValues, UpdateValues or TransformKeys

Right way to use this function

s := map[int]int{1:1, 2:2}
maps.ForEach(s, func(k, v int){ logger.Log("Map record", k, v) })

func ForEachWithErr

func ForEachWithErr[M ~map[K]V, K comparable, V any](m M, cmd func(K, V) error) error

The ForEach function exec any func for each element of map unitl error returns. !!!Pay attention, if you want to change map non ref values you should use TransformValues, UpdateValues or TransformKeys

Right way to use this function

s := map[int]int{1:1, 2:2}
maps.ForEach(s, func(k, v int) error {
	if k == 0 {
		return errors.New("Unsupported value")
	}
	logger.Log("Map record", k, v)
	return nil
})

func IntersectKeys

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

The IntersectKeys function intersect keys of maps. Function returns keys, contaning in all of maps

func IsEmpty added in v1.0.0

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

The IsEmpty function checks if collection is empty. It returns true if collection is empty

func Join

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

The Join function join values from all incoming maps to one. Source maps r not changing Equals keys override value in result map. If all values must bee saved, BucketingJoin should be used Rigth way to use

result := maps.Join(map[int]int{1:1}, map[int]int{2:1}, map[int]int{3:1})

func Keys

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

The Keys function returns slice with all keys from map. Function returns new slice with length equals to map length. Right way to use.

vals = maps.Keys(mapCollection)

func KeysSeq

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

The KeysSeq function returns iter.Seq with map keys. Right way to use.

iter = maps.KeysSeq(mapCollection)
// for _, key := ramge iter {...}

func MultiplyFunc

func MultiplyFunc[M ~map[K]V, K comparable, V any, R MathOps](m M, cmd func(K, V) R) R

The MultiplyFunc function multiply function results for each collection element. Right way to use.

val = maps.MultiplyFunc(map[int]int{1:1, 2:2}, "", func(r string, k, v int) int {return k+v})

Pay attention! It's just an example.

func NotEmpty added in v1.0.0

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

The NotEmpty function checks if collection is empty. It returns true if collection is not empty

func Reduce

func Reduce[M ~map[K]V, K comparable, V any, R any](m M, init R, cmd func(R, K, V) R) R

The Reduce function apply each of collection element to single value. Right way to use. For example to join all values in map in raw

val = maps.Reduce(map[int]int{1:1, 2:2}, "", func(r string, k, v int) string {return r + fmt.Sprintf("key: %d, val: %d; ", k, v)})

Pay attention! It's just an example, Not recommended way to join values in string raw

func Rotate

func Rotate[M ~map[K]V, K, R comparable, V any](m M, applyFunc func(K, V) R) map[R][]V

The Rotate function select new keys for each element of map. It creates new rotated map. !!!If new keys have similar values, values will be saved in slice Right way to use:

map := map[int]int{1:1, 2:2}
stringKeysMap := maps.Rotate(map, func(k, v int) string {return itoa.Itoa(k)})

func SumFunc

func SumFunc[M ~map[K]V, K comparable, V any, R MathOps](m M, cmd func(K, V) R) R

The SumFunc function sum function results for each collection element. Right way to use.

val = maps.SumFunc(map[int]int{1:1, 2:2}, "", func(r string, k, v int) int {return k*v})

Pay attention! It's just an example.

func TransformKeys

func TransformKeys[M ~map[K]V, K, R comparable, V any](m M, applyFunc func(K, V) R) map[R]V

The TransformKeys function convert each element key of map. It creates new map with updated vavlues. !!!If new keys have similar values, values will be overriten. To save all values, use Rotate function Right way to use:

map := map[int]int{1:1, 2:2}
stringKeysMap := maps.TransformKeys(map, func(k, v int) string {return itoa.Itoa(k)})

func TransformValues

func TransformValues[M ~map[K]V, K comparable, V, R any](m M, applyFunc func(K, V) R) map[K]R

The UpdateValues function convert each element value of map. It creates new map with updated vavlues. If current map values updating required, use UpdateValues Right way to use:

map := map[int]int{1:1, 2:2}
mapOfStrings := maps.TransformValues(map, func(k, v int) string {return itoa.Itoa(v)})

func UpdateValues added in v1.0.0

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

The UpdateValues function convert each element of map. It update current map values Right way to use:

map := map[int]int{1:1, 2:2}
maps.UpdateValues(map, func(k, v int) int {return v * 2})

func Values

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

The Values function returns slice with all values from map. Function returns new slice with length equals to map length. Right way to use.

vals = maps.Values(mapCollection)

func ValuesSeq

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

The ValuesSeq function returns iter.Seq with map values. Right way to use.

iter = maps.ValuesSeq(mapCollection)
// for _, val := ramge iter {...}

Types

type MathOps

type MathOps interface {
	constraints.Integer | constraints.Float | constraints.Complex
}

Jump to

Keyboard shortcuts

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