Go Set Package is a simple library for set operations with generic type supported.
go get "github.com/athom/goset"
-
Generic
All Go builtin types and custom defined types are supported. Even slice of pointers!
a := goset.Uniq([]int{1, 2, 2, 3, 3, 3, 4, 4, 4, 4}).([]int)
b := goset.Uniq([]string{"1", "2", "2", "3", "3", "3", "4"}).([]string)
type Avatar struct {
Age int
Name string
}
avatars := []Avatar{
Avatar{112, "Angg"},
Avatar{70, "Roku"},
Avatar{230, "Kyoshi"},
Avatar{230, "Kyoshi"},
Avatar{33, "Kuruk"},
Avatar{33, "Kuruk"},
Avatar{33, "Kuruk"},
}
filteredAvatars := goset.Uniq(avatars).([]Avatar)
-
Handy
One Line Style calling design, aims to be developer friendly.
But not enough shit are given on the performance and mathmatical rigour.
Think about how many times you want to tell if a element is in a slice, you have to wrote like this:
found := false
for _, e := range records {
if e == theRecord {
found = true
break
}
}
if found {
//your code
}
Now you can do this in just one line:
if goset.IsIncluded(records, theRecord) {
//your code
}
a := []int{1, 2, 3, 4, 4, 2, 3, 3, 4, 4}
ua := goset.Uniq(a).([]int)
// result: ok = false
a := []int{1, 2, 3}
b := []int{2, 1, 3}
ok := goset.IsEqual(a, b)
// result: ok = true
a := []int{1, 2, 3, 4}
ok := goset.IsIncluded(a, 1)
// result: ok = true
a := []int{1, 2, 3, 4}
a1 := []int{1, 2, 3}
ok := goset.IsSubset(a1, a)
// result: ok = true
a := []int{1, 2, 3, 4}
a1 := []int{1, 2, 3}
ok := goset.IsSuperset(a, a1)
// result: ok = true
a := []int{1, 2, 3, 4, 4, 2, 3, 3, 4, 4}
ua := goset.Uniq(a).([]int)
// result: ua = []int{1, 2, 3, 4}
a1 := []int{1, 2, 3, 4}
b1 := []int{3, 4, 5, 6}
c1 := goset.Intersect(a1, b1).([]int)
// result: c1 = []int{3, 4}
a1 := []int{1, 2, 3, 4}
b1 := []int{3, 4, 5, 6}
c1 := goset.Union(a1, b1).([]int)
// result: c1 = []int{1, 2, 3, 4, 5, 6}
a1 := []int{1, 2, 3, 4}
b1 := []int{3, 4, 5, 6}
_, _, c1, d1 := goset.Difference(a1, b1)
// result: c1 = []int{1, 2}
// d1 = []int{5, 6}
a := []int{1, 2, 3, 4}
a = goset.AddElement(a, 5).([]int)
// result: a = []int{1, 2, 3, 4, 5}
a := []int{1, 2, 3, 4}
a = goset.AddElements(a, []int{5, 6}).([]int)
// result: a = []int{1, 2, 3, 4, 5, 6}
a := []int{1, 2, 3, 4}
a = goset.RemoveElement(a, 4).([]int)
// result: a = []int{1, 2, 3}
a := []int{1, 2, 3, 4}
a = goset.RemoveElements(a, []int{3, 4}).([]int)
// result: a = []int{1, 2}
x := []int{1, 2, 3, 4}
y := goset.Map(a, func(i int) string {
return "(" + strconv.Itoa(i) + ")"
}, []string{}).([]string)
// result: y = []string{"(1)", "(2)", "(3)", "(4)"}
type Cat struct {
Id string
Name string
}
cats := []*Cat{
&Cat{
1,
"Tom",
},
&Cat{
2,
"Jerry",
},
&Cat{
3,
"HeiMao",
},
&Cat{
4,
"Coffee",
},
}
order := []int{3, 1, 4, 2}
cats = goset.Reorder(order, cats, "Id").([]*Cat)
// result:
cats = []*Cat{
&Cat{
3,
"HeiMao",
},
&Cat{
1,
"Tom",
},
&Cat{
4,
"Coffee",
},
&Cat{
2,
"Jerry",
},
}
Go Set is released under the WTFPL License.