Socket
Book a DemoInstallSign in
Socket

github.com/reactivego/immutable

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/reactivego/immutable

v0.0.2
Source
Go
Version published
Created
Source

immutable

import "github.com/reactivego/immutable"

Go Reference

Package immutable provides an immutable persistent Map type. A Map is a collection of unordered key:value pairs.

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!}
}
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
	})

	// Unordered Output:
	// first 789
	// second 456
}
package main

import (
    "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.
}
package main

import (
    "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}
}
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.}
}

FAQs

Package last updated on 25 May 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.