Documentation
¶
Overview ¶
Package set provides a generic set type and various related functions.
This package aims to provide a modern and robust implementation for sets with an familiar API. It's type safe, supports standard iterators and is feature complete. It is also fully tested and documented.
For asserting equality of sets in tests we recommend using Set.Equal as the equality comparison operator has been disabled and reflect.DeepEqual (e.g. used by assert.Equal) can give wrong results. The below example shows how to assert equality between sets correctly:
if !got.Equal(want) {
t.Errorf("got %q, wanted %q", got, want)
}
Example ¶
package main
import (
"fmt"
"github.com/ErikKalkoken/kx/set"
)
func main() {
// 1. Initialization
// Create a new set of integers
s1 := set.Of(1, 2, 3, 4)
s2 := set.Of(3, 4, 5, 6)
// 2. Basic Operations
s1.Add(7) // Add a single element
s1.Delete(1) // Remove an element
fmt.Println("Set 1:", s1) // {2 3 4 7} (Sorted in output)
fmt.Printf("Size of s1: %d\n", s1.Size())
// 3. Membership Checks
if s1.Contains(3) {
fmt.Println("Set 1 contains 3")
}
// 4. Set Algebra (Union, Intersection, Difference)
// Union: All elements from both sets
u := set.Union(s1, s2)
fmt.Println("Union:", u) // {2 3 4 5 6 7}
// Intersection: Only elements present in both sets
i := set.Intersection(s1, s2)
fmt.Println("Intersection:", i) // {3 4}
// Difference: Elements in s1 that are NOT in s2
d := set.Difference(s1, s2)
fmt.Println("Difference (s1 - s2):", d) // {2 7}
// 5. Functional & Iterator Support (Go 1.23+)
// Use DeleteFunc to remove all even numbers
s1.DeleteFunc(func(n int) bool {
return n%2 == 0
})
fmt.Println("s1 after deleting evens:", s1) // {3 7}
}
Output: Set 1: {2 3 4 7} Size of s1: 4 Set 1 contains 3 Union: {2 3 4 5 6 7} Intersection: {3 4} Difference (s1 - s2): {2 7} s1 after deleting evens: {3 7}
Index ¶
- func Max[E comparableAndOrderable](s Set[E]) E
- func MaxFunc[E comparable](s Set[E], cmp func(a, b E) int) E
- func Min[E comparableAndOrderable](s Set[E]) E
- func MinFunc[E comparable](s Set[E], cmp func(a, b E) int) E
- type Set
- func (s *Set[E]) Add(v ...E)
- func (s *Set[E]) AddSeq(seq iter.Seq[E])
- func (s Set[E]) All() iter.Seq[E]
- func (s Set[E]) Clear()
- func (s Set[E]) Clone() Set[E]
- func (s Set[E]) Contains(v E) bool
- func (s Set[E]) ContainsAll(seq iter.Seq[E]) bool
- func (s Set[E]) ContainsAny(seq iter.Seq[E]) bool
- func (s Set[E]) ContainsFunc(f func(E) bool) bool
- func (s Set[E]) Delete(v ...E) int
- func (s Set[E]) DeleteFunc(del func(E) bool) int
- func (s Set[E]) DeleteSeq(seq iter.Seq[E]) int
- func (s Set[E]) Equal(u Set[E]) bool
- func (s Set[E]) Pop() (E, bool)
- func (s Set[E]) Size() int
- func (s Set[E]) Slice() []E
- func (s Set[E]) String() string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Max ¶
func Max[E comparableAndOrderable](s Set[E]) E
Max returns the maximal value in s. It panics if s is empty.
Example ¶
package main
import (
"fmt"
"github.com/ErikKalkoken/kx/set"
)
func main() {
s := set.Of(1, 2)
fmt.Println(set.Max(s))
}
Output: 2
func MaxFunc ¶
func MaxFunc[E comparable](s Set[E], cmp func(a, b E) int) E
MaxFunc returns the maximal value in s, using cmp to compare elements. It panics if s is empty. If there is more than one maximal element according to the cmp function, MaxFunc returns the first one.
Example ¶
package main
import (
"cmp"
"fmt"
"github.com/ErikKalkoken/kx/set"
)
func main() {
s := set.Of(1, 2)
fmt.Println(set.MaxFunc(s, func(a, b int) int {
return cmp.Compare(a, b)
}))
}
Output: 2
func Min ¶
func Min[E comparableAndOrderable](s Set[E]) E
Min returns the minimal value in s. It panics if s is empty.
Example ¶
package main
import (
"fmt"
"github.com/ErikKalkoken/kx/set"
)
func main() {
s := set.Of(1, 2)
fmt.Println(set.Min(s))
}
Output: 1
func MinFunc ¶
func MinFunc[E comparable](s Set[E], cmp func(a, b E) int) E
MinFunc returns the minimal value in s, using cmp to compare elements. It panics if s is empty. If there is more than one minimal element according to the cmp function, MinFunc returns the first one.
Example ¶
package main
import (
"cmp"
"fmt"
"github.com/ErikKalkoken/kx/set"
)
func main() {
s := set.Of(1, 2)
fmt.Println(set.MinFunc(s, func(a, b int) int {
return cmp.Compare(a, b)
}))
}
Output: 1
Types ¶
type Set ¶
type Set[E comparable] struct { // contains filtered or unexported fields }
A Set is an unordered collection of unique elements.
The zero value of a Set is an empty set ready to use. Set is not safe for concurrent use.
func Collect ¶
func Collect[E comparable](seq iter.Seq[E]) Set[E]
Collect collects values from seq into a new set and returns it. If seq is empty, the result is an empty set.
Example ¶
package main
import (
"fmt"
"github.com/ErikKalkoken/kx/set"
)
func main() {
s := set.Collect(set.Of(1, 2, 3).All())
fmt.Println(s)
}
Output: {1 2 3}
func Difference ¶
func Difference[E comparable](s Set[E], others ...Set[E]) Set[E]
Difference constructs a new Set containing the elements of s that are not present in the union of others.
Example ¶
package main
import (
"fmt"
"github.com/ErikKalkoken/kx/set"
)
func main() {
s1 := set.Of(1, 2)
s2 := set.Of(2, 3)
fmt.Println(set.Difference(s1, s2))
}
Output: {1}
func Intersection ¶
func Intersection[E comparable](sets ...Set[E]) Set[E]
Intersection returns a new Set with elements common to all sets.
When less then 2 sets are provided they will be assumed to be empty.
Example ¶
package main
import (
"fmt"
"github.com/ErikKalkoken/kx/set"
)
func main() {
s1 := set.Of(1, 2)
s2 := set.Of(2, 3)
fmt.Println(set.Intersection(s1, s2))
}
Output: {2}
func Of ¶
func Of[E comparable](v ...E) Set[E]
Of returns a new set of the elements v.
Example ¶
package main
import (
"fmt"
"github.com/ErikKalkoken/kx/set"
)
func main() {
s := set.Of(1, 2, 2)
fmt.Println(s)
}
Output: {1 2}
func Union ¶
func Union[E comparable](sets ...Set[E]) Set[E]
Union returns a new Set with the elements of all sets.
Example ¶
package main
import (
"fmt"
"github.com/ErikKalkoken/kx/set"
)
func main() {
s1 := set.Of(1, 2)
s2 := set.Of(2, 3)
fmt.Println(set.Union(s1, s2))
}
Output: {1 2 3}
func (*Set[E]) Add ¶
func (s *Set[E]) Add(v ...E)
Add adds elements v to set s.
Example ¶
package main
import (
"fmt"
"github.com/ErikKalkoken/kx/set"
)
func main() {
var s set.Set[int]
s.Add(1, 2)
fmt.Println(s)
}
Output: {1 2}
func (*Set[E]) AddSeq ¶
AddSeq adds the values from seq to s.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/ErikKalkoken/kx/set"
)
func main() {
s := set.Of(1, 2)
s.AddSeq(slices.Values([]int{3, 4}))
fmt.Println(s)
}
Output: {1 2 3 4}
func (Set[E]) All ¶
All returns on iterator over all elements of set s.
Note that the order of the elements is undefined.
Example ¶
package main
import (
"fmt"
"github.com/ErikKalkoken/kx/set"
)
func main() {
s := set.Of(1, 2, 3)
for x := range s.All() {
fmt.Println(x)
}
}
Output: 1 2 3
func (Set[E]) Clear ¶
func (s Set[E]) Clear()
Clear removes all elements from set s.
Example ¶
package main
import (
"fmt"
"github.com/ErikKalkoken/kx/set"
)
func main() {
s := set.Of(1, 2)
s.Clear()
fmt.Println(s)
}
Output: {}
func (Set[E]) Clone ¶
Clone returns a new set, which contains a shallow copy of all elements of set s.
Example ¶
package main
import (
"fmt"
"github.com/ErikKalkoken/kx/set"
)
func main() {
s1 := set.Of(1, 2)
s2 := s1.Clone()
fmt.Println(s2)
}
Output: {1 2}
func (Set[E]) Contains ¶
Contains reports whether element v is in set s.
Example ¶
package main
import (
"fmt"
"github.com/ErikKalkoken/kx/set"
)
func main() {
s := set.Of(1, 2)
fmt.Println(s.Contains(2))
fmt.Println(s.Contains(3))
}
Output: true false
func (Set[E]) ContainsAll ¶
ContainsAll reports whether all of the elements in seq are in s.
Example ¶
package main
import (
"fmt"
"github.com/ErikKalkoken/kx/set"
)
func main() {
s := set.Of(1, 2)
fmt.Println(s.ContainsAll(set.Of(1).All()))
fmt.Println(s.ContainsAll(set.Of(1, 2).All()))
fmt.Println(s.ContainsAll(set.Of(1, 2, 3).All()))
}
Output: true true false
func (Set[E]) ContainsAny ¶
ContainsAny reports whether any of the elements in seq are in s.
Example ¶
package main
import (
"fmt"
"github.com/ErikKalkoken/kx/set"
)
func main() {
s := set.Of(1, 2)
fmt.Println(s.ContainsAny(set.Of(1).All()))
fmt.Println(s.ContainsAny(set.Of(1, 2).All()))
fmt.Println(s.ContainsAny(set.Of(1, 2, 3).All()))
fmt.Println(s.ContainsAny(set.Of(1, 3).All()))
fmt.Println(s.ContainsAny(set.Of(3, 4).All()))
}
Output: true true true true false
func (Set[E]) ContainsFunc ¶
ContainsFunc reports whether at least one element v of s satisfies f(v).
Example ¶
package main
import (
"fmt"
"github.com/ErikKalkoken/kx/set"
)
func main() {
s := set.Of(1, 2)
fmt.Println(s.ContainsFunc(func(x int) bool {
return x == 2
}))
fmt.Println(s.ContainsFunc(func(x int) bool {
return x == 3
}))
}
Output: true false
func (Set[E]) Delete ¶
Delete removes elements v from set s. It returns the number of deleted elements. Elements that are not found in the set are ignored.
Example ¶
package main
import (
"fmt"
"github.com/ErikKalkoken/kx/set"
)
func main() {
s := set.Of(1, 2)
s.Delete(2)
fmt.Println(s)
}
Output: {1}
func (Set[E]) DeleteFunc ¶
DeleteFunc deletes the elements in s for which del returns true. It returns the number of deleted elements.
Example ¶
package main
import (
"fmt"
"github.com/ErikKalkoken/kx/set"
)
func main() {
s := set.Of(1, 2)
s.DeleteFunc(func(x int) bool {
return x == 2
})
fmt.Println(s)
}
Output: {1}
func (Set[E]) DeleteSeq ¶
DeleteSeq deletes the elements in seq from s. Elements that are not present are ignored. It returns the number of deleted elements.
Example ¶
package main
import (
"fmt"
"github.com/ErikKalkoken/kx/set"
)
func main() {
s := set.Of(1, 2, 3)
s.DeleteSeq(set.Of(2, 3, 4).All())
fmt.Println(s)
}
Output: {1}
func (Set[E]) Equal ¶
Equal reports whether sets s and u are equal. A zero set will be reported equal to an (initialized) empty set.
Example ¶
package main
import (
"fmt"
"github.com/ErikKalkoken/kx/set"
)
func main() {
s := set.Of(1, 2)
fmt.Println(s.Equal(set.Of(1, 2)))
fmt.Println(s.Equal(set.Of(1, 3)))
}
Output: true false
func (Set[E]) Pop ¶
Pop tries to remove and return an arbitrary element from s and reports whether it was successful.
Example ¶
package main
import (
"fmt"
"github.com/ErikKalkoken/kx/set"
)
func main() {
s := set.Of(1)
v, ok := s.Pop()
fmt.Println(v, ok)
_, ok = s.Pop()
fmt.Println(ok)
}
Output: 1 true false
func (Set[E]) Size ¶
Size returns the number of elements in set s. An empty set returns 0.
Example ¶
package main
import (
"fmt"
"github.com/ErikKalkoken/kx/set"
)
func main() {
s := set.Of(1, 2, 3)
fmt.Println(s.Size())
}
Output: 3
func (Set[E]) Slice ¶
func (s Set[E]) Slice() []E
Slice creates a new slice from the elements of set s and returns it.
Note that the order of elements is undefined.
Example ¶
package main
import (
"fmt"
"github.com/ErikKalkoken/kx/set"
)
func main() {
s := set.Of(1, 2, 3)
for _, x := range s.Slice() {
fmt.Println(x)
}
}
Output: 1 2 3