Documentation
¶
Overview ¶
Package set provides a generic Set type.
Index ¶
- func Fold[T comparable, State any](folder func(State, T) State, initial State, s Set[T]) State
- func FoldBack[T comparable, State any](folder func(T, State) State, s Set[T], initial State) State
- func MaxElement[T constraints.Ordered](s Set[T]) (T, error)
- func MaxElementBy[T comparable, R constraints.Ordered](projection func(T) R, s Set[T]) (T, error)
- func MinElement[T constraints.Ordered](s Set[T]) (T, error)
- func MinElementBy[T comparable, R constraints.Ordered](projection func(T) R, s Set[T]) (T, error)
- type Set
- func DifferenceMany[T comparable](sets ...Set[T]) Set[T]
- func FromSlice[T comparable](input []T, lessFunc ...func(T, T) int) Set[T]
- func IntersectMany[T comparable](sets ...Set[T]) Set[T]
- func Map[T comparable, R comparable](mapping func(T) R, s Set[T]) Set[R]
- func NewSet[T comparable](lessFunc ...func(T, T) int) Set[T]
- func Singleton[T comparable](item T) Set[T]
- func UnionMany[T comparable](sets ...Set[T]) Set[T]
- func (s *Set[T]) Add(item T)
- func (s Set[T]) Contains(item T) bool
- func (s Set[T]) Count() int
- func (s Set[T]) Difference(set2 Set[T]) Set[T]
- func (s Set[T]) Equal(s2 Set[T]) bool
- func (s Set[T]) Exists(predicate func(T) bool) bool
- func (s Set[T]) Filter(predicate func(T) bool) Set[T]
- func (s Set[T]) ForAll(predicate func(T) bool) bool
- func (s Set[T]) IndexOf(item T) int
- func (s Set[T]) Intersect(set2 Set[T]) Set[T]
- func (s Set[T]) IsEmpty() bool
- func (s Set[T]) IsProperSubsetOf(potentialSuperset Set[T]) bool
- func (s Set[T]) IsProperSupersetOf(potentialSubset Set[T]) bool
- func (s Set[T]) IsSubsetOf(potentialSuperset Set[T]) bool
- func (s Set[T]) IsSupersetOf(potentialSubset Set[T]) bool
- func (s Set[T]) Items() []T
- func (s Set[T]) Iter(action func(T))
- func (s Set[T]) Iteri(action func(int, T))
- func (s Set[T]) Partition(predicate func(T) bool) (trueSet Set[T], falseSet Set[T])
- func (s *Set[T]) Remove(item T)
- func (s Set[T]) ToSlice() []T
- func (s Set[T]) Union(set2 Set[T]) Set[T]
Examples ¶
- DifferenceMany
- FromSlice
- IntersectMany
- Map
- MaxElement
- MaxElementBy
- MinElement
- MinElementBy
- NewSet
- Set.Contains
- Set.Difference
- Set.Equal
- Set.Exists
- Set.Filter
- Set.IndexOf
- Set.Intersect
- Set.IsProperSubsetOf
- Set.IsProperSupersetOf
- Set.IsSubsetOf
- Set.IsSupersetOf
- Set.Iter
- Set.Iteri
- Set.Partition
- Set.Union
- Singleton
- UnionMany
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Fold ¶
func Fold[T comparable, State any](folder func(State, T) State, initial State, s Set[T]) State
Fold applies the folder function to each item in the Set until it reaches the final state.
func FoldBack ¶
func FoldBack[T comparable, State any](folder func(T, State) State, s Set[T], initial State) State
FoldBack applies the folder function in reverse order to each item in the Set until it reaches the final state.
func MaxElement ¶
func MaxElement[T constraints.Ordered](s Set[T]) (T, error)
MaxElement finds the largest item in the Set s.
Example ¶
s := FromSlice([]int{1, 2, 3, 4})
r, _ := MaxElement(s)
fmt.Println(r)
Output: 4
func MaxElementBy ¶
func MaxElementBy[T comparable, R constraints.Ordered](projection func(T) R, s Set[T]) (T, error)
MaxElementBy finds the largest item in the Set s using the return values from projection for comparison.
Example ¶
s := FromSlice([]int{1, 2, 3, 4})
r, _ := MaxElementBy(func(v int) int {
if v < 3 {
return v * 10
}
return v
}, s)
fmt.Println(r)
Output: 2
func MinElement ¶
func MinElement[T constraints.Ordered](s Set[T]) (T, error)
MinElement finds the smallest item in the Set s.
Example ¶
s := FromSlice([]int{1, 2, 3, 4})
r, _ := MinElement(s)
fmt.Println(r)
Output: 1
func MinElementBy ¶
func MinElementBy[T comparable, R constraints.Ordered](projection func(T) R, s Set[T]) (T, error)
MinElementBy finds the smallest item in the Set s using the return values from projection for comparison.
Example ¶
s := FromSlice([]int{1, 2, 3, 4})
r, _ := MinElementBy(func(v int) int {
if v < 3 {
return v * 10
}
return v
}, s)
fmt.Println(r)
Output: 3
Types ¶
type Set ¶
type Set[T comparable] struct { // contains filtered or unexported fields }
Set is a type keeps a set of values and allows set operations on them.
func DifferenceMany ¶
func DifferenceMany[T comparable](sets ...Set[T]) Set[T]
DifferenceMany finds the difference between all the sets.
Example ¶
s := FromSlice([]int{1, 2, 3, 4})
s2 := FromSlice([]int{1, 2, 3, 4, 5, 6})
s3 := FromSlice([]int{6, 7})
r := DifferenceMany(s, s2, s3)
fmt.Println(r.Items())
Output: [5 7]
func FromSlice ¶
func FromSlice[T comparable](input []T, lessFunc ...func(T, T) int) Set[T]
FromSlice creates a new Set from the items in the given slice.
Example ¶
s := FromSlice([]string{"one", "two", "one"})
s2 := FromSlice([]int{1, 3, 4, 2, 4, 3, 2}, func(a, b int) int {
if a < b {
return -1
}
if b < a {
return 1
}
return 0
})
fmt.Println(s.Items(), s2.Items())
Output: [one two] [1 2 3 4]
func IntersectMany ¶
func IntersectMany[T comparable](sets ...Set[T]) Set[T]
ItersectMany finds the intersection between all the sets.
Example ¶
s := FromSlice([]int{1, 2, 3, 4})
s2 := FromSlice([]int{1, 2, 3, 4, 5, 6})
s3 := FromSlice([]int{3, 6, 7})
r := IntersectMany(s, s2, s3)
fmt.Println(r.Items())
Output: [3]
func Map ¶
func Map[T comparable, R comparable](mapping func(T) R, s Set[T]) Set[R]
Map applies the mapping function to each item in the Set s.
Example ¶
s := FromSlice([]int{1, 2, 3, 4})
s2 := Map(func(v int) string { return strconv.Quote(strconv.Itoa(v)) }, s)
fmt.Println(s2.Items())
Output: ["1" "2" "3" "4"]
func NewSet ¶
func NewSet[T comparable](lessFunc ...func(T, T) int) Set[T]
NewSet creates a new Set. If lessFunc is provided, it is used in ordering the set.
Example ¶
less := func(a, b int) int {
if a > b {
return -1
}
if b < a {
return 1
}
return 0
}
s := NewSet(less)
s.Add(1)
s.Add(2)
s.Add(4)
s.Add(3)
fmt.Println(s.Items())
Output: [4 3 2 1]
func Singleton ¶
func Singleton[T comparable](item T) Set[T]
Singleton creates a set of exactly one item. This does not actually prevent you from adding more items later, so this is a way to create a set and add the first item in one call.
Example ¶
s := Singleton(1) fmt.Println(s.Items())
Output: [1]
func UnionMany ¶
func UnionMany[T comparable](sets ...Set[T]) Set[T]
UnionMany finds the union between all the sets.
Example ¶
s := FromSlice([]int{1, 2, 3, 4})
s2 := FromSlice([]int{1, 2, 3, 4, 5, 6})
s3 := FromSlice([]int{6, 7})
r := UnionMany(s, s2, s3)
fmt.Println(r.Items())
Output: [1 2 3 4 5 6 7]
func (*Set[T]) Add ¶
func (s *Set[T]) Add(item T)
Add adds an item to the Set. If it already exists in the set, nothing changes.
func (Set[T]) Contains ¶
Contains tests whether item is present in the Set.
Example ¶
s := FromSlice([]string{"one", "two", "one"})
fmt.Println(s.Contains("two"), s.Contains("three"))
Output: true false
func (Set[T]) Difference ¶
Difference returns a Set containing the items that are only present in one Set or the other.
Example ¶
s := FromSlice([]string{"one", "two", "one"})
s2 := FromSlice([]string{"one", "two", "one", "three"})
r := s.Difference(s2)
fmt.Println(r.Items())
Output: [three]
func (Set[T]) Equal ¶
Equal tests if two sets are equal.
Example ¶
s := FromSlice([]string{"one", "two", "one"})
s2 := FromSlice([]string{"two", "one", "one"})
fmt.Println(s.Equal(s2))
Output: true
func (Set[T]) Exists ¶
Exists tests whether any item in the Set matches the predicate.
Example ¶
s := FromSlice([]string{"one", "two", "one"})
r := s.Exists(func(s string) bool { return strings.HasPrefix(s, "o") })
r2 := s.Exists(func(s string) bool { return strings.HasPrefix(s, "s") })
fmt.Println(r, r2)
Output: true false
func (Set[T]) Filter ¶
Filter returns a Set that contains only items from this Set that match the predicate.
Example ¶
s := FromSlice([]int{1, 2, 3, 4})
r := s.Filter(func(i int) bool { return i%2 == 0 })
fmt.Println(r.Items())
Output: [2 4]
func (Set[T]) IndexOf ¶
IndexOf finds the index of the item within the Set. The order of items within the Set is the order in which the items were added or sorted order if a comparison function was supplied when this Set was created.
Example ¶
s := NewSet(func(a, b int) int {
if a < b {
return -1
}
if b < a {
return 1
}
return 0
})
s.Add(1)
s.Add(3)
s.Add(2)
fmt.Println(s.IndexOf(3))
Output: 2
func (Set[T]) Intersect ¶
Intersect returns a Set containing the items that are present in both Sets.
Example ¶
s := FromSlice([]string{"one", "two", "one"})
s2 := FromSlice([]string{"one", "three"})
r := s.Intersect(s2)
fmt.Println(r.Items())
Output: [one]
func (Set[T]) IsProperSubsetOf ¶
IsProperSubsetOf tests whether this Set is a subset of the Set passed as a parameter. A Set is a subset of another Set if it only contains items that are also present in the other Set. An empty Set is not considered a subset of any other Set. Also, if the Sets are equal, neither is considered to a subset of the other. If you do want these cases to be considered a subset, use IsSubsetOf instead.
Example ¶
s := FromSlice([]string{"one", "two", "one"})
s2 := FromSlice([]string{"one", "two", "one", "three"})
s3 := FromSlice([]string{"one", "two", "one", "three"})
fmt.Println(s.IsProperSubsetOf(s2), s2.IsProperSubsetOf(s), s2.IsProperSubsetOf(s3))
Output: true false false
func (Set[T]) IsProperSupersetOf ¶
IsProperSupersetOf tests whether this Set is a superset of the Set passed as a parameter. A Set is a superset of another Set if it contains all items that are present in the other Set. If either Set is empty or if they are equal, this will not consider the Set to be a superset. If you want these cases to be considered as supersets, use IsSupersetOf instead.
Example ¶
s := FromSlice([]string{"one", "two", "one"})
s2 := FromSlice([]string{"one", "two", "one", "three"})
s3 := FromSlice([]string{"one", "two", "one", "three"})
fmt.Println(s.IsProperSupersetOf(s2), s2.IsProperSupersetOf(s), s2.IsProperSupersetOf(s3))
Output: false true false
func (Set[T]) IsSubsetOf ¶
IsSubsetOf tests whether this Set is a subset of the Set passed as a parameter. A Set is a subset of another Set if it only contains items that are also present in the other Set. An empty Set is considered a subset of any other Set. This also considers equal sets to be subsets of each other. If you do not want these cases to be considered a subset, use IsProperSubsetOf instead.
Example ¶
s := FromSlice([]string{"one", "two", "one"})
s2 := FromSlice([]string{"one", "two", "one", "three"})
fmt.Println(s.IsSubsetOf(s2), s2.IsSubsetOf(s))
Output: true false
func (Set[T]) IsSupersetOf ¶
IsSupersetOf tests whether this Set is a superset of the Set passed as a parameter. A Set is a superset of another Set if it contains all items that are present in the other Set. If the other Set is empty or if they are equal, this will consider the Set to be a superset. If you do not want these cases to be considered as supersets, use IsProperSupersetOf instead.
Example ¶
s := FromSlice([]string{"one", "two", "one"})
s2 := FromSlice([]string{"one", "two", "one", "three"})
fmt.Println(s.IsSupersetOf(s2), s2.IsSupersetOf(s))
Output: false true
func (Set[T]) Iter ¶
func (s Set[T]) Iter(action func(T))
Iter applies the action to each item in the Set.
Example ¶
s := FromSlice([]int{1, 2, 3, 4})
s.Iter(func(item int) { fmt.Print(item) })
Output: 1234
func (Set[T]) Iteri ¶
Iteri applies the action to each item in the Set, also passing the index to the action.
Example ¶
s := FromSlice([]int{1, 2, 3, 4})
s.Iteri(func(i, item int) { fmt.Print(i, item) })
Output: 0 11 22 33 4
func (Set[T]) Partition ¶
Partition returns two Sets. The first Set are those items in this Set that match the predicate. The second Set are those items in this Set that do not match the predicate.
Example ¶
s := FromSlice([]int{1, 2, 3, 4})
t, f := s.Partition(func(i int) bool { return i%2 == 0 })
fmt.Println(t.Items(), f.Items())
Output: [2 4] [1 3]
func (Set[T]) ToSlice ¶
func (s Set[T]) ToSlice() []T
ToSlice returns the values in the Set as a slice of items.