基于泛型的 slice 伪集合操作,封装常见的 union、interaction 和 difference 等函数。
Standard go get
:
$ go get -v -u github.com/chensanle/sliset
// Difference res = base - compared
func Difference(base, compared []E1) []E1
// Intersection res = base ∩ compared
func Intersection(base, compared []E1) []E1
// Union res = base U compared
func Union(base, compared []E1) []E1
// Uniq remove duplicate elements from the base.
func Uniq(base []E1) []E1
r1 := sliset.Difference([]string{"apple", "fb", "ali"}, []string{"apple"})
fmt.Println("output: ", r1)
// output: []string{"ali"}
r2 := sliset.Difference([]int{1949, 1997, 2008}, []int{2008})
fmt.Println("output: ", r2)
// output: []string{1949, 1997}
r1 := sliset.Uniq([]string{"apple", "fb", "ali", "apple"})
fmt.Println("output: ", r1)
// output: []string{"apple", "fb", "ali"}
r2 := sliset.Uniq([]int{1949, 1997, 2008, 1949})
fmt.Println("output: ", r2)
// output: []string{1949, 1997, 2008}