Files
google-labs-jules[bot] 86720090b6 docs: Add GoDoc to pkg and improve markdown files (#3251)
This change adds GoDoc comments to many of the public symbols in the
`pkg/` directory. It also includes various improvements to the
documentation in `README.md` and other markdown files in the `docs/`
directory.

This is a partial documentation effort, as requested by the user, to
get a pull request submitted quickly.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
2025-09-22 19:37:15 +02:00

46 lines
857 B
Go

package set
import (
"maps"
"slices"
"golang.org/x/exp/constraints"
)
// SortedKeys returns the sorted keys of the map.
// The keys are sorted in ascending order.
func SortedKeys[K constraints.Ordered, V any](m map[K]V) []K {
// sort
keys := maps.Keys(m)
return slices.Sorted(keys)
}
// Sorted returns a sorted set of the input.
// Duplicates are removed.
func Sorted[K constraints.Ordered](l []K) []K {
return SortedFiltered(l, func(k K) bool {
return true
})
}
// SortedFiltered returns a sorted set of the input, filtered by the predicate.
// Duplicates are removed.
func SortedFiltered[K constraints.Ordered](l []K, want func(K) bool) []K {
if len(l) == 0 {
return l
}
// deduplicate
m := make(map[K]struct{}, len(l))
for _, k := range l {
if !want(k) {
continue
}
m[k] = struct{}{}
}
// sort
return SortedKeys(m)
}