Documentation ¶
Overview ¶
Example (Map) ¶
package main import ( "fmt" "github.com/reactivego/immutable" ) func main() { var m immutable.Map[string, string] m = m.Set("Hello", "World!") fmt.Println(m) }
Output: {Hello:World!}
Index ¶
- Constants
- Variables
- type Map
- func (a Map[K, V]) Del(key K) Map[K, V]
- func (a Map[K, V]) Depth() int
- func (a Map[K, V]) Get(key K) V
- func (a Map[K, V]) Has(key K) bool
- func (a Map[K, V]) Len() int
- func (a Map[K, V]) Lookup(key K) (V, bool)
- func (a Map[K, V]) Range(f func(K, V) bool)
- func (a Map[K, V]) Set(key K, value V) Map[K, V]
- func (a Map[K, V]) String() string
- type MapError
- type MapX
- func (a MapX[K, V]) Del(key K) MapX[K, V]
- func (a MapX[K, V]) Depth() int
- func (a MapX[K, V]) Get(key K) V
- func (a MapX[K, V]) Has(key K) bool
- func (a MapX[K, V]) Len() int
- func (a MapX[K, V]) Lookup(key K) (V, bool)
- func (a MapX[K, V]) Range(f func(K, V) bool)
- func (a MapX[K, V]) Set(key K, value V) MapX[K, V]
- func (a MapX[K, V]) String() string
- type Set
- type Store
- func (a Store[D, K, V]) Del(data D) Store[D, K, V]
- func (a Store[D, K, V]) Depth() int
- func (a Store[D, K, V]) Get(data D) any
- func (a Store[D, K, V]) Has(data D) bool
- func (a Store[D, K, V]) Len() int
- func (a Store[D, K, V]) Put(data D) Store[D, K, V]
- func (a Store[D, K, V]) Range(f func(K, V) bool)
- func (a Store[D, K, V]) String() string
Examples ¶
Constants ¶
const UnhashableKeyType = MapError("Unhashable Key Type")
Variables ¶
var EnableHashCollision = false
Functions ¶
This section is empty.
Types ¶
type Map ¶
type Map[K comparable, V any] struct { // contains filtered or unexported fields }
Map is a persistent immutable hash array mapped trie (HAMT) with an internal hash function. The key types it supports are either string or any integer type. Keys are directly compared using the '==' operator. Key types other than string or integers need an external hasher.
Example ¶
package main import ( "fmt" "github.com/reactivego/immutable" ) func main() { var m immutable.Map[string, int] m = m.Set("first", 123).Set("second", 456).Set("first", 789) m.Range(func(key string, value int) bool { fmt.Println(key, value) return true }) }
Output: first 789 second 456
func (Map[K, V]) Del ¶ added in v0.0.2
Del returns a copy of the Map with the entry for the key removed.
func (Map[K, V]) Depth ¶ added in v0.0.2
Depth returns the number of levels in the amt. Calling Depth on an empty amt returns 1.
func (Map[K, V]) Get ¶ added in v0.0.2
func (a Map[K, V]) Get(key K) V
Get returns the value for the entry with the given key or zero value when it is not present.
func (Map[K, V]) Has ¶ added in v0.0.2
Has returns true when an entry with the given key is present.
func (Map[K, V]) Lookup ¶ added in v0.0.2
Lookup returns the value of an entry associated with a given key along with the value true when the key is present. Otherwise it returns (zero, false).
func (Map[K, V]) Range ¶ added in v0.0.2
Range calls the given function for every key,value pair present.
type MapX ¶ added in v0.0.2
type MapX[K comparable, V any] struct { // contains filtered or unexported fields }
MapX is a persistent immutable hash array mapped trie (HAMT) with an external key marshal function. The marshal function will map the key to a byte slice. The byteslice is passed to the internal hash function for the Map. The key itself is stored in the Map verbatim and actually used in compare operations. The marshaled key is only used for hashing.
Example ¶
package main import ( "encoding/json" "fmt" "github.com/reactivego/immutable" ) func main() { // Key is comparable (i.e. == and !=) but not hashable. // Notice that the key is a struct with unexported fields that are not // marshalled by the default json.Marshal function. type key struct{ A, B, c int } // Map with a marshal function to convert the key to []byte for hashing. m := immutable.MapWith[key, string](json.Marshal) m = m.Set(key{1, 2, 3}, "Mammalia is the class of mammals.") m = m.Set(key{1, 2, 4}, "Aves is the class of birds.") b, e := json.Marshal(key{1, 2, 3}) fmt.Println(string(b), e) fmt.Println(m.Get(key{1, 2, 3})) fmt.Println(m.Get(key{1, 2, 4})) // same hash as key{1, 2, 3} because 'c' is not exported. fmt.Println(m.Get(key{7, 8, 9})) }
Output: {"A":1,"B":2} <nil> Mammalia is the class of mammals. Aves is the class of birds.
func (MapX[K, V]) Del ¶ added in v0.0.2
Del returns a copy of the Map with the entry for the key removed.
func (MapX[K, V]) Depth ¶ added in v0.0.2
Depth returns the number of levels in the Map. Calling Depth on an empty amt returns 1.
func (MapX[K, V]) Get ¶ added in v0.0.2
func (a MapX[K, V]) Get(key K) V
Get returns the value for the entry with the given key or nil when it is not present.
func (MapX[K, V]) Has ¶ added in v0.0.2
Has returns true when an entry with the given key is present.
func (MapX[K, V]) Lookup ¶ added in v0.0.2
Lookup returns the value of an entry associated with a given key along with the value true when the key is present. Otherwise it returns (nil, false).
func (MapX[K, V]) Range ¶ added in v0.0.2
Range calls the given function for every key,value pair present.
func (MapX[K, V]) Set ¶ added in v0.0.2
Set returns a copy of the Map with the given key,value pair inserted.
Example ¶
package main import ( "encoding/json" "fmt" "github.com/reactivego/immutable" ) func main() { // Key is comparable (i.e. == and !=) but not hashable. type Key struct{ K1, K2 int64 } type Topic struct{ Name, Description string } // Map with a marshal function to convert the key to []byte for hashing. m := immutable.MapWith[Key, Topic](json.Marshal) m = m.Set(Key{1, 2}, Topic{"Theme", "This is a topic about theme"}) fmt.Println(m) fmt.Println(m.Get(Key{1, 2})) }
Output: {{K1:1 K2:2}:{Name:Theme Description:This is a topic about theme}} {Theme This is a topic about theme}
type Set ¶ added in v0.0.2
type Set[K comparable] struct { // contains filtered or unexported fields }
func (Set[K]) Depth ¶ added in v0.0.2
Depth returns the number of levels in the Set. Calling Depth on an empty amt returns 1.
type Store ¶ added in v0.0.2
type Store[D any, K comparable, V any] struct { // contains filtered or unexported fields }
Store is a Hash Array Mapped Trie with an external split function.
func StoreWith ¶ added in v0.0.2
func StoreWith[D any, K comparable, V any](splitter func(D) (K, V)) Store[D, K, V]
func (Store[D, K, V]) Del ¶ added in v0.0.2
Del returns a copy of the Store with the key removed from the set.
func (Store[D, K, V]) Depth ¶ added in v0.0.2
Depth returns the number of levels in the Hamt. Calling Depth on an empty Hamt returns 1.
func (Store[D, K, V]) Get ¶ added in v0.0.2
Get returns the value for the entry with the given key or nil when it is not present.
func (Store[D, K, V]) Has ¶ added in v0.0.2
Has returns true when an entry with the given key is present.
func (Store[D, K, V]) Put ¶ added in v0.0.2
Put returns a copy of the Set with the key as part of the set.
Example ¶
package main import ( "fmt" "github.com/reactivego/immutable" ) func main() { // Topic is an example of data where the key (i.e. Name) is part of the data. type Topic struct{ Name, Description string } store := immutable.StoreWith(func(t Topic) (string, Topic) { return t.Name, t }) store = store.Put(Topic{Name: "Aves", Description: "This topic is about birds."}) store = store.Put(Topic{Name: "Mammalia", Description: "This topic is about mammals"}) fmt.Printf("%+v\n", store.Get(Topic{Name: "Mammalia"})) fmt.Printf("%+v\n", store.Get(Topic{Name: "Aves"})) }
Output: {Name:Mammalia Description:This topic is about mammals} {Name:Aves Description:This topic is about birds.}