1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
[](https://travis-ci.org/adam-hanna/arrayOperations) [](https://coveralls.io/github/adam-hanna/arrayOperations?branch=master) [](https://goreportcard.com/report/github.com/adam-hanna/arrayOperations) [](https://godoc.org/github.com/adam-hanna/arrayOperations)
# arrayOperations
Small library for performing union, intersect, difference and distinct operations on slices in goLang
I don't promise that these are optimized (not even close!), but they work :)
## Quickstart
~~~ go
var a = []int{1, 1, 2, 3}
var b = []int{2, 4}
z, ok := Difference(a, b)
if !ok {
fmt.Println("Cannot find difference")
}
slice, ok := z.Interface().([]int)
if !ok {
fmt.Println("Cannot convert to slice")
}
fmt.Println(slice, reflect.TypeOf(slice)) // [1, 3, 4] []int
~~~
## API
### [Difference](https://godoc.org/github.com/adam-hanna/arrayOperations#Difference)
~~~ go
func Difference(arrs ...interface{}) (reflect.Value, bool)
~~~
Difference returns a slice of values that are only present in one of the input slices
[1, 2, 2, 4, 6] & [2, 4, 5] >> [1, 5, 6]
[1, 1, 3, 4, 5, 6] >> [1, 3, 4, 5, 6]
~~~ go
// EXAMPLE
var a = []int{1, 1, 2, 3}
var b = []int{2, 4}
z, ok := Difference(a, b)
if !ok {
fmt.Println("Cannot find difference")
}
slice, ok := z.Interface().([]int)
if !ok {
fmt.Println("Cannot convert to slice")
}
fmt.Println(slice, reflect.TypeOf(slice)) // [1, 3, 4] []int
~~~
### [Distinct](https://godoc.org/github.com/adam-hanna/arrayOperations#Distinct)
~~~ go
func Distinct(arr interface{}) (reflect.Value, bool)
~~~
Distinct returns the unique vals of a slice
[1, 1, 2, 3] >> [1, 2, 3]
~~~ go
// EXAMPLE
var a = []int{1, 1, 2, 3}
z, ok := Distinct(a)
if !ok {
fmt.Println("Cannot find distinct")
}
slice, ok := z.Interface().([]int)
if !ok {
fmt.Println("Cannot convert to slice")
}
fmt.Println(slice, reflect.TypeOf(slice)) // [1, 2, 3] []int
~~~
### [Intersect](https://godoc.org/github.com/adam-hanna/arrayOperations#Intersect)
~~~ go
func Intersect(arrs ...interface{}) (reflect.Value, bool)
~~~
Intersect returns a slice of values that are present in all of the input slices
[1, 1, 3, 4, 5, 6] & [2, 3, 6] >> [3, 6]
[1, 1, 3, 4, 5, 6] >> [1, 3, 4, 5, 6]
~~~ go
// EXAMPLE
var a = []int{1, 1, 2, 3}
var b = []int{2, 4}
z, ok := Intersect(a, b)
if !ok {
fmt.Println("Cannot find intersect")
}
slice, ok := z.Interface().([]int)
if !ok {
fmt.Println("Cannot convert to slice")
}
fmt.Println(slice, reflect.TypeOf(slice)) // [2] []int
~~~
### [Union](https://godoc.org/github.com/adam-hanna/arrayOperations#Union)
~~~ go
func Union(arrs ...interface{}) (reflect.Value, bool)
~~~
Union returns a slice that contains the unique values of all the input slices
[1, 2, 2, 4, 6] & [2, 4, 5] >> [1, 2, 4, 5, 6]
[1, 1, 3, 4, 5, 6] >> [1, 3, 4, 5, 6]
~~~ go
// EXAMPLE
var a = []int{1, 1, 2, 3}
var b = []int{2, 4}
z, ok := Union(a, b)
if !ok {
fmt.Println("Cannot find union")
}
slice, ok := z.Interface().([]int)
if !ok {
fmt.Println("Cannot convert to slice")
}
fmt.Println(slice, reflect.TypeOf(slice)) // [1, 2, 3, 4] []int
~~~
## License
MIT
|