Documentation
¶
Overview ¶
Package xmaps contains utilities for working with maps.
Index ¶
- func Difference[S ~map[T]struct{}, T comparable](a, b S) S
- func FromKeysAndValues[K comparable, V any](keys []K, values []V) (map[K]V, bool)
- func Intersection[S ~map[T]struct{}, T comparable](sets ...S) S
- func Intersects[S ~map[T]struct{}, T comparable](sets ...S) bool
- func Reverse[M ~map[K]V, K comparable, V comparable](m M) map[V][]K
- func ReverseSingle[M ~map[K]V, K comparable, V comparable](m M) (map[V]K, bool)
- func ToIndex[K comparable](keys []K) map[K]int
- func Union[S ~map[T]struct{}, T comparable](sets ...S) S
- type Set
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Difference ¶
func Difference[S ~map[T]struct{}, T comparable](a, b S) S
Difference returns all items of a that do not appear in b.
Example ¶
package main import ( "fmt" "github.com/bradenaw/juniper/xmaps" ) func main() { a := xmaps.Set[int]{ 1: {}, 4: {}, 5: {}, } b := xmaps.Set[int]{ 3: {}, 4: {}, } difference := xmaps.Difference(a, b) fmt.Println(difference) }
Output: map[1:{} 5:{}]
func FromKeysAndValues ¶
func FromKeysAndValues[K comparable, V any](keys []K, values []V) (map[K]V, bool)
FromKeysAndValues returns a map from keys[i] to values[i]. If there are any duplicate keys, the resulting map has an arbitrary choice of the associated values and the second return is false. It panics if len(keys)!=len(values).
func Intersection ¶
func Intersection[S ~map[T]struct{}, T comparable](sets ...S) S
Intersection returns a set of the items that all input sets have in common.
Example ¶
package main import ( "fmt" "github.com/bradenaw/juniper/xmaps" ) func main() { a := xmaps.Set[int]{ 1: {}, 2: {}, 4: {}, } b := xmaps.Set[int]{ 1: {}, 3: {}, 4: {}, } c := map[int]struct{}{ 1: {}, 4: {}, 5: {}, } intersection := xmaps.Intersection(a, b, c) fmt.Println(intersection) }
Output: map[1:{} 4:{}]
func Intersects ¶
func Intersects[S ~map[T]struct{}, T comparable](sets ...S) bool
Intersects returns true if the input sets have any element in common.
Example ¶
package main import ( "fmt" "github.com/bradenaw/juniper/xmaps" ) func main() { a := xmaps.Set[int]{ 1: {}, 2: {}, } b := xmaps.Set[int]{ 1: {}, 3: {}, } c := xmaps.Set[int]{ 3: {}, 4: {}, } fmt.Println(xmaps.Intersects(a, b)) fmt.Println(xmaps.Intersects(b, c)) fmt.Println(xmaps.Intersects(a, c)) }
Output: true true false
func Reverse ¶
func Reverse[M ~map[K]V, K comparable, V comparable](m M) map[V][]K
Reverse returns a map from m's values to each of the keys that mapped to it in arbitrary order.
Example ¶
package main import ( "fmt" "github.com/bradenaw/juniper/xmaps" ) func main() { a := map[string]int{ "foo": 2, "bar": 1, "baz": 2, } reversed := xmaps.Reverse(a) fmt.Println(1, reversed[1][0]) fmt.Println(2, reversed[2][0]) fmt.Println(2, reversed[2][1]) }
Output: 1 bar 2 foo 2 baz
func ReverseSingle ¶
func ReverseSingle[M ~map[K]V, K comparable, V comparable](m M) (map[V]K, bool)
ReverseSingle returns a map of m's values to m's keys. If there are any duplicate values, the resulting map has an arbitrary choice of the associated keys and the second return is false.
Example ¶
package main import ( "fmt" "github.com/bradenaw/juniper/xmaps" ) func main() { a := map[string]int{ "foo": 1, "bar": 2, "baz": 3, } reversed, ok := xmaps.ReverseSingle(a) fmt.Println(ok) fmt.Println(reversed) }
Output: true map[1:foo 2:bar 3:baz]
func ToIndex ¶
func ToIndex[K comparable](keys []K) map[K]int
ToIndex returns a map from keys[i] to i.
Example ¶
package main import ( "fmt" "github.com/bradenaw/juniper/xmaps" ) func main() { m := []string{"foo", "bar", "baz"} fmt.Println(xmaps.ToIndex(m)) }
Output: map[bar:1 baz:2 foo:0]
func Union ¶
func Union[S ~map[T]struct{}, T comparable](sets ...S) S
Union returns a set containing all elements of all input sets.
Example ¶
package main import ( "fmt" "github.com/bradenaw/juniper/xmaps" ) func main() { a := xmaps.Set[int]{ 1: {}, 4: {}, } b := xmaps.Set[int]{ 3: {}, 4: {}, } c := xmaps.Set[int]{ 1: {}, 5: {}, } union := xmaps.Union(a, b, c) fmt.Println(union) }
Output: map[1:{} 3:{} 4:{} 5:{}]
Types ¶
type Set ¶
type Set[T comparable] map[T]struct{}
Set[T] is shorthand for map[T]struct{} with convenience methods.
func SetFromSlice ¶
func SetFromSlice[T comparable](items []T) Set[T]
SetFromSlice returns a Set whose elements are items.