Documentation
¶
Overview ¶
Package stringset provides the capabilities of a standard Set with strings, including the standard set operations and a specialized ability to have a "negative" set for intersections.
Negative sets are useful for inverting the sense of a set when combining them using Intersection. Negative sets do not change the behavior of any other set operations, including Union and Difference. Negative operations were created for managing a set of tags, where it is useful to be able to search for items that contain some tags but do not contain others.
The API is designed to be chainable so that common operations become one-liners, and it is designed to be easy to use with slices of strings.
This package does not return errors. Set operations should be fast and chainable; adding items more than once, or attempting to remove things that don't exist are not errors.
Index ¶
- type StringSet
- func (ss *StringSet) Add(sa ...string) *StringSet
- func (ss *StringSet) Clone() *StringSet
- func (ss *StringSet) Contains(s string) bool
- func (ss *StringSet) Delete(sa ...string) *StringSet
- func (ss *StringSet) Difference(ss2 *StringSet) *StringSet
- func (ss *StringSet) Equals(ss2 *StringSet) bool
- func (ss *StringSet) Filter(f func(s string) bool) *StringSet
- func (ss *StringSet) Intersection(ss2 *StringSet) *StringSet
- func (ss *StringSet) Join(sep string) string
- func (ss *StringSet) Length() int
- func (ss *StringSet) Negate() *StringSet
- func (ss *StringSet) Strings() []string
- func (ss *StringSet) Union(ss2 *StringSet) *StringSet
- func (ss *StringSet) WrappedJoin(prefix string, sep string, suffix string) string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type StringSet ¶
type StringSet struct {
IsNegative bool
// contains filtered or unexported fields
}
A StringSet is implemented using a map[string]nothing -- strings are copied when added to the set.
func (*StringSet) Add ¶
Add puts one or more strings into the set. If a string is already present the set is unchanged.
Example ¶
package main
import (
"fmt"
"sort"
"github.com/AchievementNetwork/stringset"
)
func main() {
greetings := stringset.New().Add("hello")
greetings.Add("aloha", "bonjour", "g'day")
// the Strings() function will return results in random order
output := greetings.Strings()
sort.Strings(output)
fmt.Println(output)
}
Output: [aloha bonjour g'day hello]
func (*StringSet) Delete ¶
Delete removes one or more items from a set if they exist in the set.
Example ¶
package main
import (
"fmt"
"sort"
"github.com/AchievementNetwork/stringset"
)
func main() {
nums := stringset.New().Add("1", "2", "3", "4", "5", "6", "7")
nums.Delete("2", "4", "6")
output := nums.Strings()
sort.Strings(output)
fmt.Println(output)
}
Output: [1 3 5 7]
func (*StringSet) Difference ¶
Difference is an assymetric set difference. It subtracts the rhs from the lhs and returns a new set. IsNegative has no effect.
func (*StringSet) Equals ¶
Equals checks whether two string sets have the same members. IsNegative has no effect.
func (*StringSet) Filter ¶
Filter returns a new StringSet that contains only the members of the original set where f(member) evaluates to true. Negate has no effect.
func (*StringSet) Intersection ¶
Intersection returns the symmetric difference of the two sets, with the possibility that either or both sets may be negative. It's the equivalent of a boolean AND with either or both of the targets being inverted.
This returns a new set -- both operands are left unchanged.
abc & cde == c
abc & !cde == ab
!abc & cde == de
!abc & !cde == !abcde
Example ¶
package main
import (
"fmt"
"sort"
"github.com/AchievementNetwork/stringset"
)
func main() {
ss1 := stringset.New().Add("a", "b", "c", "d")
ss2 := stringset.New().Add("c", "d", "e", "f")
inter := ss1.Intersection(ss2)
output := inter.Strings()
sort.Strings(output)
fmt.Println(output)
}
Output: [c d]
func (*StringSet) Join ¶
Join returns all the items in the set as a single string, in an arbitrarily-ordered list, separated by a separator.
func (*StringSet) Negate ¶
Negate flips the IsNegative bit.
Example ¶
package main
import (
"fmt"
"sort"
"github.com/AchievementNetwork/stringset"
)
func main() {
ss1 := stringset.New().Add("a", "b", "c")
ss2 := stringset.New().Add("c", "d", "e").Negate()
// set 2 is negative so its elements will be deleted from set 1
inter1 := ss1.Intersection(ss2)
output1 := inter1.Strings()
sort.Strings(output1)
fmt.Println(output1)
ss1.Negate()
ss2.Negate()
// set 1 is negative so its elements will be deleted from set 2
inter2 := ss1.Intersection(ss2)
output2 := inter2.Strings()
sort.Strings(output2)
fmt.Println(output2)
}
Output: [a b] [d e]