Documentation ¶
Overview ¶
Package maps provides generic functions for maps.
The package is inspired by the `Map` Elixir module.
Index ¶
- Variables
- func Clear[M ~map[K]V, K comparable, V any](items M)
- func Copy[M ~map[K]V, K comparable, V any](items M) M
- func Drop[M ~map[K]V, K comparable, V any](items M, keys ...K)
- func Equal[M1, M2 ~map[K]V, K, V comparable](items1 M1, items2 M2) bool
- func HasKey[M ~map[K]V, K comparable, V any](items M, key K) bool
- func Keys[M ~map[K]V, K comparable, V any](items M) []K
- func LeaveOnly[M ~map[K]V, K comparable, V any](items M, keys ...K)
- func Map[M ~map[K]V, K, RK comparable, V, RV any](items M, f func(K, V) (RK, RV)) map[RK]RV
- func MapKeys[M ~map[K]V, K, RK comparable, V any](items M, f func(K) RK) map[RK]V
- func MapValues[M ~map[K]V, K comparable, V, RV any](items M, f func(V) RV) map[K]RV
- func Merge[M1, M2 ~map[K]V, K, V comparable](items1 M1, items2 M2) M1
- func MergeBy[M1, M2 ~map[K]V, K, V comparable](items1 M1, items2 M2, f func(K, V, V) V) M1
- func Pop[M ~map[K]V, K comparable, V any](items M, key K) (V, error)
- func Replace[M ~map[K]V, K comparable, V any](items M, key K, value V)
- func Take[M ~map[K]V, K comparable, V any](items M, keys ...K) M
- func Update[M1, M2 ~map[K]V, K, V comparable](items M1, with M2)
- func Values[M ~map[K]V, K comparable, V any](items M) []V
- func Without[M ~map[K]V, K comparable, V any](items M, keys ...K) M
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("given element is not found")
ErrNotFound is an error for case when given element is not found
Functions ¶
func Clear ¶
func Clear[M ~map[K]V, K comparable, V any](items M)
Clear removes all items from the given map
This is an in-place operation, meaning that it modifies the original map.
func Copy ¶
func Copy[M ~map[K]V, K comparable, V any](items M) M
Copy returns a copy of the given map
func Drop ¶
func Drop[M ~map[K]V, K comparable, V any](items M, keys ...K)
Drop removes the given keys from the given map.
If a key is not found in the given map, it is simply ignored.
It is in-place operation, it modifies the original map.
func Equal ¶
func Equal[M1, M2 ~map[K]V, K, V comparable](items1 M1, items2 M2) bool
Equal compares the two given maps.
Both maps should have the same length, keys, and values.
func HasKey ¶
func HasKey[M ~map[K]V, K comparable, V any](items M, key K) bool
HasKey returns true if the given map contains the given key.
func Keys ¶
func Keys[M ~map[K]V, K comparable, V any](items M) []K
Keys returns the keys of the given map.
The resulting keys order is unknown. You cannot rely on it in any way, even on it being random.
func LeaveOnly ¶
func LeaveOnly[M ~map[K]V, K comparable, V any](items M, keys ...K)
LeaveOnly leaves in the given map only the given keys.
func Map ¶
func Map[M ~map[K]V, K, RK comparable, V, RV any](items M, f func(K, V) (RK, RV)) map[RK]RV
Map calls the given function f with each key and value and makes a new map out of key-value pairs returned by the function.
func MapKeys ¶
func MapKeys[M ~map[K]V, K, RK comparable, V any](items M, f func(K) RK) map[RK]V
MapKeys is like Map but the function f accepts only a key and returns only the new key.
func MapValues ¶
func MapValues[M ~map[K]V, K comparable, V, RV any](items M, f func(V) RV) map[K]RV
MapValues is like Map but the function f accepts only a value and returns only the new value.
func Merge ¶
func Merge[M1, M2 ~map[K]V, K, V comparable](items1 M1, items2 M2) M1
Merge returns a new map containing items from both given maps.
In case of duplicate values, the value from items2 has precedence over items1.
func MergeBy ¶
func MergeBy[M1, M2 ~map[K]V, K, V comparable](items1 M1, items2 M2, f func(K, V, V) V) M1
MergeBy is like Merge but conflicts are resolved by the function f.
func Pop ¶
func Pop[M ~map[K]V, K comparable, V any](items M, key K) (V, error)
Pop removes the given key from the map and returns the associated value.
If the key not found, the ErrNotFound error is returned.
The function modifies the original map in-place.
func Replace ¶
func Replace[M ~map[K]V, K comparable, V any](items M, key K, value V)
Replace replaces the `key` by the `value` but only if the key is already in the map.
func Take ¶
func Take[M ~map[K]V, K comparable, V any](items M, keys ...K) M
Take returns a copy of the given map containing only the given keys.
Keys that aren't in the map are simply ignored.
This function is a counterpart for `Without`.
func Update ¶
func Update[M1, M2 ~map[K]V, K, V comparable](items M1, with M2)
Update merges the given `with` map into `items` map.
This is an in-place operation. It modifies `items` map in-place.
func Values ¶
func Values[M ~map[K]V, K comparable, V any](items M) []V
Values returns the values of the given map.
The resulting values order is unknown. You cannot rely on it in any way, even on it being random.
func Without ¶
func Without[M ~map[K]V, K comparable, V any](items M, keys ...K) M
Without returns a copy of the given map without the given keys.
If a key is not found in the given map, it is simply ignored.
Types ¶
This section is empty.