romaps

package
v0.0.0-...-fe7a19e Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2022 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package romaps defines various read-only functions useful with immutable maps of any type.

Example
package main

import (
	"fmt"

	"github.com/phelmkamp/immut/romaps"
)

func main() {
	m1 := romaps.Freeze(map[string]int{"foo": 42, "bar": 7})
	m2 := map[string]int{"fiz": 3}
	fmt.Println(len(romaps.Keys(m1)))
	romaps.Copy(m2, m1)
	fmt.Println(m2)
}
Output:

2
map[bar:7 fiz:3 foo:42]
Example (Context)

Example_context demonstrates that a read-only Map can be used as a value in context.Context.

package main

import (
	"context"
	"fmt"

	"github.com/phelmkamp/immut/romaps"
)

func main() {
	type ctxKey string
	cfgKey := ctxKey("cfg")
	ctx := context.WithValue(context.Background(), cfgKey, romaps.Freeze(map[string]string{
		"foo": "42",
		"bar": "baz",
	}))

	cfg, _ := ctx.Value(cfgKey).(romaps.Map[string, string])
	if !cfg.IsNil() {
		v, _ := cfg.Index("foo")
		fmt.Println(v)
	}
}
Output:

42

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clone

func Clone[K comparable, V any](m Map[K, V]) map[K]V

Clone returns a mutable copy of m. This is a shallow clone: the new keys and values are set using ordinary assignment.

func Copy

func Copy[K comparable, V any](dst map[K]V, src Map[K, V])

Copy copies all key/value pairs in src adding them to dst. When a key in src is already present in dst, the value in dst will be overwritten by the value associated with the key in src.

func Equal

func Equal[K, V comparable](m1 Map[K, V], m2 Map[K, V]) bool

Equal reports whether two maps contain the same key/value pairs. Values are compared using ==.

func EqualFunc

func EqualFunc[K comparable, V1, V2 any](m1 Map[K, V1], m2 Map[K, V2], eq func(V1, V2) bool) bool

EqualFunc is like Equal, but compares values using eq. Keys are still compared with ==.

func Keys

func Keys[K comparable, V any](m Map[K, V]) []K

Keys returns the keys of the map m. The keys will be in an indeterminate order.

func Values

func Values[K comparable, V any](m Map[K, V]) []V

Values returns the values of the map m. The values will be in an indeterminate order.

Types

type Map

type Map[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Map wraps a read-only map.

func Freeze

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

Freeze returns a read-only wrapper for the given map.

func (Map[K, V]) Do

func (m Map[K, V]) Do(f func(k K, v V) bool)

Do calls f on every pair, stopping if f returns false. f will be called on values in an indeterminate order.

func (Map[K, V]) Index

func (m Map[K, V]) Index(k K) (v V, ok bool)

Index returns the value associated with k. The boolean value ok is true if the value v corresponds to a key found in the map, false if it is a zero value because the key was not found.

func (Map[K, V]) IsNil

func (m Map[K, V]) IsNil() bool

IsNil reports whether the underlying map is nil.

func (Map[K, V]) Len

func (m Map[K, V]) Len() int

Len returns the length.

func (Map[K, V]) String

func (m Map[K, V]) String() string

String returns the underlying map formatted as a string.

Jump to

Keyboard shortcuts

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