mapsutil

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package mapsutil provides utilities for maps.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func SortedByKey

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

SortedByKey returns an iter.Seq2 of the key-value pairs of the map, sorted by ordered keys . If a key is deleted from the map while the sequence is being iterated, it will be skipped. If a key is added to the map while the sequence is being iterated, it will not be included in the sequence.

Example
m := map[string]int{
	"c": 3,
	"a": 1,
	"b": 2,
}
for k, v := range SortedByKey(m) {
	fmt.Println(k, v)
}
Output:

a 1
b 2
c 3

func SortedByKeyFunc

func SortedByKeyFunc[M ~map[K]V, K comparable, V any](m M, cmpFunc func(K, K) int) iter.Seq2[K, V]

SortedByKeyFunc is like SortedByKey but the keys are sorted using the provided comparison function.

Example
m := map[string]int{
	"c": 3,
	"a": 1,
	"b": 2,
}
for k, v := range SortedByKeyFunc(m, func(a, b string) int {
	return -strings.Compare(a, b)
}) {
	fmt.Println(k, v)
}
Output:

c 3
b 2
a 1

Types

This section is empty.

Jump to

Keyboard shortcuts

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