maps

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2022 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.

  1. Copy
  2. Equal
  3. HasKey
  4. Map
  5. MapKeys
  6. MapValues
  7. Merge
  8. MergeBy
  9. Keys
  10. Take
  11. Values
  12. Without
  13. Clear
  14. Drop
  15. LeaveOnly
  16. Pop
  17. Replace
  18. Update

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.

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.

Jump to

Keyboard shortcuts

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