Documentation
¶
Overview ¶
Package set contains generic typesafe set operations.
Index ¶
- type Of
- func (s Of[T]) Add(vals ...T)
- func (s Of[T]) AddSeq(inp iter.Seq[T])
- func (s Of[T]) All() iter.Seq[T]
- func (s Of[T]) Del(vals ...T)
- func (s Of[T]) Each(f func(T))
- func (s Of[T]) Eachx(f func(T) error) error
- func (s Of[T]) Equal(other Of[T]) bool
- func (s Of[T]) Has(val T) bool
- func (s Of[T]) Len() int
- func (s Of[T]) Slice() []T
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Of ¶
type Of[T comparable] map[T]struct{}
Of is a set of elements of type T. It is called "Of" so that when qualified with this package name and instantiated with a member type, it reads naturally: e.g., set.Of[int].
The zero value of Of is not safe for use. Create one with New instead.
Example ¶
package main import ( "fmt" "github.com/bobg/go-generics/v4/set" ) func main() { s := set.New(1, 2, 3, 4, 5) fmt.Println("1 is in the set?", s.Has(1)) fmt.Println("100 is in the set?", s.Has(100)) s.Add(100) fmt.Println("100 is in the set?", s.Has(100)) fmt.Println("set size is", s.Len()) s.Del(100) fmt.Println("100 is in the set?", s.Has(100)) fmt.Println("set size is", s.Len()) }
Output: 1 is in the set? true 100 is in the set? false 100 is in the set? true set size is 6 100 is in the set? false set size is 5
func Collect ¶
func Collect[T comparable](inp iter.Seq[T]) Of[T]
Collect collects the members of the given sequence into a new set.
func Diff ¶
func Diff[T comparable](s1, s2 Of[T]) Of[T]
Diff produces a new set containing the items in s1 that are not also in s2. Either set may be nil. The result is never nil (but may be empty).
Example ¶
package main import ( "fmt" "github.com/bobg/go-generics/v4/set" ) func main() { var ( s1 = set.New(1, 2, 3, 4, 5) s2 = set.New(4, 5, 6, 7, 8) diff = set.Diff(s1, s2) ) diff.Each(func(val int) { fmt.Println(val) }) }
Output: 1 2 3
func Intersect ¶
func Intersect[T comparable](sets ...Of[T]) Of[T]
Intersect produces a new set containing only items that appear in all the given sets. The input may include nils, representing empty sets and therefore producing an empty (but non-nil) intersection.
Example ¶
package main import ( "fmt" "github.com/bobg/go-generics/v4/set" ) func main() { var ( s1 = set.New(1, 2, 3, 4, 5) s2 = set.New(4, 5, 6, 7, 8) inter = set.Intersect(s1, s2) ) inter.Each(func(val int) { fmt.Println(val) }) }
Output: 4 5
func New ¶
func New[T comparable](vals ...T) Of[T]
New produces a new set containing the given values.
func Union ¶
func Union[T comparable](sets ...Of[T]) Of[T]
Union produces a new set containing all the items in all the given sets. The input may include nils, representing empty sets. The result is never nil (but may be empty).
Example ¶
package main import ( "fmt" "github.com/bobg/go-generics/v4/set" ) func main() { var ( s1 = set.New(1, 2, 3, 4, 5) s2 = set.New(4, 5, 6, 7, 8) union = set.Union(s1, s2) ) union.Each(func(val int) { fmt.Println(val) }) }
Output: 1 2 3 4 5 6 7 8
func (Of[T]) Add ¶
func (s Of[T]) Add(vals ...T)
Add adds the given values to the set. Items already present in the set are silently ignored.
func (Of[T]) All ¶
All produces an iterator over the members of the set, in an indeterminate order. The set may be nil.
func (Of[T]) Del ¶
func (s Of[T]) Del(vals ...T)
Del removes the given items from the set. Items already absent from the set are silently ignored.
func (Of[T]) Each ¶
func (s Of[T]) Each(f func(T))
Each calls a function on each element of the set in an indeterminate order. It is safe to add and remove items during a call to Each, but that can affect the sequence of values seen later during the same Each call. The set may be nil.
func (Of[T]) Eachx ¶
Eachx calls a function on each element of the set in an indeterminate order. It is safe to add and remove items during a call to Each, but that can affect the sequence of values seen later during the same Eachx call. The set may be nil. If the function returns an error, Eachx stops and returns that error.
func (Of[T]) Equal ¶
Equal tests whether the set has the same membership as another. Either set may be nil.