Skip to content
/ goset Public

A library for set operations with generic type supported

Notifications You must be signed in to change notification settings

athom/goset

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Set

Gitter

Go Set Package is a simple library for set operations with generic type supported.

Build Status GoDoc

Installation

	go get "github.com/athom/goset"

Features

  • 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
}

Useage

Detections

1. IsUniq
a := []int{1, 2, 3, 4, 4, 2, 3, 3, 4, 4}
ua := goset.Uniq(a).([]int)
// result: ok = false
2. IsEqual
a := []int{1, 2, 3}
b := []int{2, 1, 3}
ok := goset.IsEqual(a, b)
// result: ok = true
3. IsIncluded
a := []int{1, 2, 3, 4}
ok := goset.IsIncluded(a, 1)
// result: ok = true
4. IsSubset
a := []int{1, 2, 3, 4}
a1 := []int{1, 2, 3}
ok := goset.IsSubset(a1, a)
// result: ok = true
5. IsSuperset
a := []int{1, 2, 3, 4}
a1 := []int{1, 2, 3}
ok := goset.IsSuperset(a, a1)
// result: ok = true

Operations

1. Uniq
a := []int{1, 2, 3, 4, 4, 2, 3, 3, 4, 4}
ua := goset.Uniq(a).([]int)
// result: ua = []int{1, 2, 3, 4}
2. Intersect
a1 := []int{1, 2, 3, 4}
b1 := []int{3, 4, 5, 6}
c1 := goset.Intersect(a1, b1).([]int)
// result: c1 = []int{3, 4}
3. Union
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}
4. Difference
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}
5. AddElement
a := []int{1, 2, 3, 4}
a = goset.AddElement(a, 5).([]int)
// result: a = []int{1, 2, 3, 4, 5}
6. AddElements
a := []int{1, 2, 3, 4}
a = goset.AddElements(a, []int{5, 6}).([]int)
// result: a = []int{1, 2, 3, 4, 5, 6}
7. RemoveElement
a := []int{1, 2, 3, 4}
a = goset.RemoveElement(a, 4).([]int)
// result: a = []int{1, 2, 3}
8. RemoveElements
a := []int{1, 2, 3, 4}
a = goset.RemoveElements(a, []int{3, 4}).([]int)
// result: a = []int{1, 2}
9. Map
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)"}
10. Reorder
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",
        },
}

License

Go Set is released under the WTFPL License.

Bitdeli Badge

About

A library for set operations with generic type supported

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages