maps

package
v0.2.16 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: MIT Imports: 4 Imported by: 0

README

maps

import "github.com/gechr/x/maps"

Package maps provides map helpers: sorted iteration, grouping, and inversion.

Index

func Group

func Group[K comparable, V any](seq iter.Seq2[K, V]) map[K][]V

Group collects the pairs of seq into a map of slices, grouping values by key in encounter order.

Example
words := []string{"apple", "banana", "avocado", "blueberry", "cherry"}
byInitial := xmaps.Group(func(yield func(byte, string) bool) {
    for _, w := range words {
        if !yield(w[0], w) {
            return
        }
    }
})
for initial, group := range xmaps.Sorted(byInitial) {
    fmt.Printf("%c: %v\n", initial, group)
}

Output:

a: [apple avocado]
b: [banana blueberry]
c: [cherry]

func GroupFunc

func GroupFunc[K comparable, V any](seq iter.Seq[V], key func(V) K) map[K][]V

GroupFunc collects the values of seq into a map of slices, grouping values in encounter order by the key returned by key.

Example
words := []string{"go", "rust", "zig", "java", "c"}
byLength := xmaps.GroupFunc(slices.Values(words), func(w string) int {
    return len(w)
})
for length, group := range xmaps.Sorted(byLength) {
    fmt.Println(length, group)
}

Output:

1 [c]
2 [go]
3 [zig]
4 [rust java]

func Invert

func Invert[M ~map[K]V, K, V comparable](m M) map[V]K

Invert returns a new map with the keys and values of m swapped. If multiple keys map to the same value, exactly one of them survives as the value in the result, chosen arbitrarily due to map iteration order.

Example
codes := map[string]int{"a": 1, "b": 2, "c": 3}
letters := xmaps.Invert(codes)
for code, letter := range xmaps.Sorted(letters) {
    fmt.Println(code, letter)
}

Output:

1 a
2 b
3 c

func KeysSlice

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

KeysSlice returns the keys of m as a slice, in indeterminate order.

Example
m := map[string]int{"charlie": 3, "alpha": 1, "beta": 2}
keys := xmaps.KeysSlice(m)
slices.Sort(keys)
fmt.Println(strings.Join(keys, ", "))

Output:

alpha, beta, charlie

func Sorted

func Sorted[M ~map[K]V, K cmp.Ordered, V any](m M) iter.Seq2[K, V]

Sorted returns an iterator over the entries of m in ascending key order.

Example
m := map[string]int{"charlie": 3, "alpha": 1, "beta": 2}
for k, v := range xmaps.Sorted(m) {
    fmt.Println(k, v)
}

Output:

alpha 1
beta 2
charlie 3

func SortedFunc

func SortedFunc[M ~map[K]V, K comparable, V any](m M, compare func(x, y K) int) iter.Seq2[K, V]

SortedFunc returns an iterator over the entries of m in the key order determined by compare, which follows the cmp.Compare convention.

Example

SortedFunc accepts any comparison following the cmp.Compare convention, such as a descending key order.

m := map[int]string{1: "one", 2: "two", 3: "three"}
descending := func(x, y int) int { return cmp.Compare(y, x) }
for k, v := range xmaps.SortedFunc(m, descending) {
    fmt.Println(k, v)
}

Output:

3 three
2 two
1 one

func ValuesSlice

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

ValuesSlice returns the values of m as a slice, in indeterminate order.

Example
m := map[string]int{"charlie": 3, "alpha": 1, "beta": 2}
values := xmaps.ValuesSlice(m)
slices.Sort(values)
fmt.Println(values)

Output:

[1 2 3]

Documentation

Overview

Package maps provides map helpers: sorted iteration, grouping, and inversion.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Group

func Group[K comparable, V any](seq iter.Seq2[K, V]) map[K][]V

Group collects the pairs of `seq` into a map of slices, grouping values by key in encounter order.

Example
package main

import (
	"fmt"

	xmaps "github.com/gechr/x/maps"
)

func main() {
	words := []string{"apple", "banana", "avocado", "blueberry", "cherry"}
	byInitial := xmaps.Group(func(yield func(byte, string) bool) {
		for _, w := range words {
			if !yield(w[0], w) {
				return
			}
		}
	})
	for initial, group := range xmaps.Sorted(byInitial) {
		fmt.Printf("%c: %v\n", initial, group)
	}
}
Output:
a: [apple avocado]
b: [banana blueberry]
c: [cherry]

func GroupFunc

func GroupFunc[K comparable, V any](seq iter.Seq[V], key func(V) K) map[K][]V

GroupFunc collects the values of `seq` into a map of slices, grouping values in encounter order by the key returned by `key`.

Example
package main

import (
	"fmt"
	"slices"

	xmaps "github.com/gechr/x/maps"
)

func main() {
	words := []string{"go", "rust", "zig", "java", "c"}
	byLength := xmaps.GroupFunc(slices.Values(words), func(w string) int {
		return len(w)
	})
	for length, group := range xmaps.Sorted(byLength) {
		fmt.Println(length, group)
	}
}
Output:
1 [c]
2 [go]
3 [zig]
4 [rust java]

func Invert

func Invert[M ~map[K]V, K, V comparable](m M) map[V]K

Invert returns a new map with the keys and values of `m` swapped. If multiple keys map to the same value, exactly one of them survives as the value in the result, chosen arbitrarily due to map iteration order.

Example
package main

import (
	"fmt"

	xmaps "github.com/gechr/x/maps"
)

func main() {
	codes := map[string]int{"a": 1, "b": 2, "c": 3}
	letters := xmaps.Invert(codes)
	for code, letter := range xmaps.Sorted(letters) {
		fmt.Println(code, letter)
	}
}
Output:
1 a
2 b
3 c

func KeysSlice

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

KeysSlice returns the keys of `m` as a slice, in indeterminate order.

Example
package main

import (
	"fmt"
	"slices"
	"strings"

	xmaps "github.com/gechr/x/maps"
)

func main() {
	m := map[string]int{"charlie": 3, "alpha": 1, "beta": 2}
	keys := xmaps.KeysSlice(m)
	slices.Sort(keys)
	fmt.Println(strings.Join(keys, ", "))
}
Output:
alpha, beta, charlie

func Sorted

func Sorted[M ~map[K]V, K cmp.Ordered, V any](m M) iter.Seq2[K, V]

Sorted returns an iterator over the entries of `m` in ascending key order.

Example
package main

import (
	"fmt"

	xmaps "github.com/gechr/x/maps"
)

func main() {
	m := map[string]int{"charlie": 3, "alpha": 1, "beta": 2}
	for k, v := range xmaps.Sorted(m) {
		fmt.Println(k, v)
	}
}
Output:
alpha 1
beta 2
charlie 3

func SortedFunc

func SortedFunc[M ~map[K]V, K comparable, V any](m M, compare func(x, y K) int) iter.Seq2[K, V]

SortedFunc returns an iterator over the entries of `m` in the key order determined by `compare`, which follows the cmp.Compare convention.

Example

SortedFunc accepts any comparison following the cmp.Compare convention, such as a descending key order.

package main

import (
	"cmp"
	"fmt"

	xmaps "github.com/gechr/x/maps"
)

func main() {
	m := map[int]string{1: "one", 2: "two", 3: "three"}
	descending := func(x, y int) int { return cmp.Compare(y, x) }
	for k, v := range xmaps.SortedFunc(m, descending) {
		fmt.Println(k, v)
	}
}
Output:
3 three
2 two
1 one

func ValuesSlice

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

ValuesSlice returns the values of `m` as a slice, in indeterminate order.

Example
package main

import (
	"fmt"
	"slices"

	xmaps "github.com/gechr/x/maps"
)

func main() {
	m := map[string]int{"charlie": 3, "alpha": 1, "beta": 2}
	values := xmaps.ValuesSlice(m)
	slices.Sort(values)
	fmt.Println(values)
}
Output:
[1 2 3]

Types

This section is empty.

Jump to

Keyboard shortcuts

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