docs: document remaining packages and simplify AST types #45

Merged
mvhutz merged 15 commits from docs/rest into main 2026-02-10 01:15:42 +00:00
Showing only changes of commit 2a028d95ec - Show all commits

View File

@@ -1,31 +1,41 @@
// Package set defines a generic, mutable unordered set data structure.
package set package set
import "iter" import "iter"
// A Set is an implementation of an mutable, unordered set. It uses a Golang map
// as its underlying data structure.
type Set[T comparable] map[T]bool type Set[T comparable] map[T]bool
// Add appends a list of items into the set.
func (s Set[T]) Add(items ...T) { func (s Set[T]) Add(items ...T) {
for _, item := range items { for _, item := range items {
s[item] = true s[item] = true
} }
} }
// Has returns true an item is present in the set.
func (s Set[T]) Has(item T) bool { func (s Set[T]) Has(item T) bool {
return s[item] return s[item]
} }
// Remove deletes a list of items from the set.
func (s Set[T]) Remove(items ...T) { func (s Set[T]) Remove(items ...T) {
for _, item := range items { for _, item := range items {
delete(s, item) delete(s, item)
} }
} }
// Merge adds all items in the argument into the set. The argument is not
// mutated.
func (s Set[T]) Merge(o Set[T]) { func (s Set[T]) Merge(o Set[T]) {
for item := range o { for item := range o {
s.Add(item) s.Add(item)
} }
} }
// ToList returns all items present in the set, as a slice. The order of the
// items is not guaranteed.
func (s Set[T]) ToList() []T { func (s Set[T]) ToList() []T {
list := []T{} list := []T{}
@@ -36,6 +46,8 @@ func (s Set[T]) ToList() []T {
return list return list
} }
// Items returns a sequence of all items present in the set. The order of the
// items is not guaranteed.
func (s Set[T]) Items() iter.Seq[T] { func (s Set[T]) Items() iter.Seq[T] {
return func(yield func(T) bool) { return func(yield func(T) bool) {
for item := range s { for item := range s {
@@ -46,6 +58,7 @@ func (s Set[T]) Items() iter.Seq[T] {
} }
} }
// New creates a set of all items as argument.
func New[T comparable](items ...T) Set[T] { func New[T comparable](items ...T) Set[T] {
result := Set[T]{} result := Set[T]{}