maps

package
v1.10.3 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: MIT Imports: 1 Imported by: 6

README ¶

genesis/maps

Package maps provides generic functions for maps.

The package is inspired by the Map Elixir module.

Documentation ¶

Overview ¶

🗺 Package maps provides generic functions for maps.

The package is inspired by the `Map` Elixir module.

Index ¶

Constants ¶

This section is empty.

Variables ¶

View Source
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. If you want to create a new map, use Without instead.

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 FromKeys ¶ added in v1.4.0

func FromKeys[K comparable, V any](keys []K, value V) map[K]V

FromKeys makes a new map with keys taken from `keys` array and all values equal to `value`.

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 HasValue ¶ added in v1.4.0

func HasValue[M ~map[K]V, K, V comparable](items M, val V) bool

HasValue returns true if the given map contains the given value.

func IMapValues ¶ added in v1.4.0

func IMapValues[M ~map[K]V, K comparable, V any](items M, f func(V) V)

IMapValues applies the function to the map values.

This is an in-place operation. It modifies `items` map in-place. If you want to create a new map, use MapValues instead.

func IMerge ¶ added in v1.4.0

func IMerge[M1, M2 ~map[K]V, K, V comparable](target M1, items M2)

IMerge adds items from the second map to the first one.

This is an in-place operation. It modifies `target` map in-place.

func IMergeBy ¶ added in v1.4.0

func IMergeBy[M1, M2 ~map[K]V, K, V comparable](target M1, items M2, f func(K, V, V) V)

IMergeBy is like IMerge but conflicts are resolved by the function `f`.

This is an in-place operation. It modifies `target` map in-place.

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.

It is in-place operation, it modifies the original map. If you want to create a new map, use Take instead.

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.

If you want to modify the map in-place, use IMerge instead.

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`.

If you want to modify the map in-place, use IMergeBy instead.

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.

If the key is already in the map, the function does nothing.

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. If you want to modify the map in-place, use LeaveOnly instead.

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.

This function is a counterpart for Take. If you want to modify the map in-place, use Drop instead.

Types ¶

This section is empty.

Jump to

Keyboard shortcuts

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