Documentation
¶
Overview ¶
mapx - a package with common map functions, like keys, values, merge, etc.
Construction ¶
This section contains functions that help with map construction, like creating a map from a list of values, or merging maps.
Construction - Compose ¶
Compose creates a map from a list, getting key/value one by one.
Usage:
mapx.Compose("a", 1, "b", 2) // map[any]any{"a": 1, "b": 2}
Construction - Merge ¶
Merge merges multiple maps into one.
Usage:
mapx.Merge( // map[string]int{"a": 1, "b": 3, "c": 4} map[string]int{"a": 1, "b": 2}, map[string]int{"b": 3, "c": 4}, )
Transformation ¶
This section contains functions that transform a map into another object.
Transformation - Keys ¶
Keys returns a list of keys from a map.
Usage:
mapx.Keys(map[string]int{"a": 1, "b": 2}) // []string{"a", "b"}
Transformation - Values ¶
Values returns a list of values from a map.
Usage:
mapx.Values(map[string]int{"a": 1, "b": 2}) // []int{1, 2}
Modification ¶
This section contains functions that modify an existing map.
Modification - Keep ¶
Keep keeps only keys that are in the list.
Usage:
mapx.Keep(map[string]int{"a": 1, "b": 2}, "a") // map[string]int{"a": 1}
Modification - Delete ¶
Delete removes keys from a map.
Usage:
mapx.Delete(map[string]int{"a": 1, "b": 2}, "a") // map[string]int{"b": 2}
Index ¶
- func Compose(vals ...any) map[any]any
- func Delete[T1 comparable, T2 any](m map[T1]T2, keys ...T1) map[T1]T2
- func Keep[T1 comparable, T2 any](m map[T1]T2, keys ...T1) map[T1]T2
- func Keys[T1 comparable, T2 any](m map[T1]T2) (keys []T1)
- func Merge[T1 comparable, T2 any](maps ...map[T1]T2) map[T1]T2
- func Values[T1 comparable, T2 any](m map[T1]T2) (values []T2)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Compose ¶
Compose makes a map with the given keys and values. Useful as a template function to pass multiple values to a template. Based on even and odd values.
Usage:
// Code mapx.Compose("foo", 1, "bar", 2) // map[any]any{"foo": 1, "bar": 2}
func Delete ¶
func Delete[T1 comparable, T2 any](m map[T1]T2, keys ...T1) map[T1]T2
Delete removes given keys from a given map.
Usage:
example := map[int]int{1: 2, 5: 6, 8: 10} mapx.Delete(example, 5, 8) // map[int]int{1: 2}
func Keep ¶
func Keep[T1 comparable, T2 any](m map[T1]T2, keys ...T1) map[T1]T2
Keep reverses logic of mapx.Delete. Removes keys from a given map, if key not found in the given keys.
Usage:
example := map[int]int{1: 2, 5: 6, 8: 10} mapx.Keep(example, 5, 8) // map[int]int{5: 6, 8: 10}
func Keys ¶
func Keys[T1 comparable, T2 any](m map[T1]T2) (keys []T1)
Keys extracts keys from a given map.
Usage:
example := map[int]int{1: 2, 5: 6, 8: 10} mapx.Keys(example) // []int{1, 5, 8}
func Merge ¶
func Merge[T1 comparable, T2 any](maps ...map[T1]T2) map[T1]T2
Merge merges given maps into single map. Each next map overrides previous one keys.
Usage:
res := mapx.Merge( // map[string]int{"a": 1, "b": 3, "c": 4} map[string]int{"a": 1, "b": 2}, map[string]int{"b": 3, "c": 4}, )
func Values ¶
func Values[T1 comparable, T2 any](m map[T1]T2) (values []T2)
Values extracts values from a given map.
Usage:
example := map[int]int{1: 2, 5: 6, 8: 10} mapx.Values(example) // []int{2, 6, 10}
Types ¶
This section is empty.