From 2a028d95ecd60ae7138329f258f3c51a065e1532 Mon Sep 17 00:00:00 2001 From: "M.V. Hutz" Date: Sat, 7 Feb 2026 21:10:27 -0500 Subject: [PATCH] docs: set --- pkg/set/set.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pkg/set/set.go b/pkg/set/set.go index c66cf71..2e2ee59 100644 --- a/pkg/set/set.go +++ b/pkg/set/set.go @@ -1,31 +1,41 @@ +// Package set defines a generic, mutable unordered set data structure. package set 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 +// Add appends a list of items into the set. func (s Set[T]) Add(items ...T) { for _, item := range items { s[item] = true } } +// Has returns true an item is present in the set. func (s Set[T]) Has(item T) bool { return s[item] } +// Remove deletes a list of items from the set. func (s Set[T]) Remove(items ...T) { for _, item := range items { 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]) { for item := range o { 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 { list := []T{} @@ -36,6 +46,8 @@ func (s Set[T]) ToList() []T { 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] { return func(yield func(T) bool) { 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] { result := Set[T]{}