Skip to content

Commit

Permalink
Merge branch 'hotfix/0.0.25'
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-hanna committed Apr 13, 2017
2 parents fd3be05 + b79fc2f commit 2936c12
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 11 deletions.
111 changes: 109 additions & 2 deletions README.md
Expand Up @@ -5,8 +5,7 @@ Small library for performing union, intersect, difference and distinct operation

I don't promise that these are optimized (not even close!), but they work :)

## Usage

## Quickstart
~~~ go
var a = []int{1, 1, 2, 3}
var b = []int{2, 4}
Expand All @@ -23,5 +22,113 @@ if !ok {
fmt.Println(slice, reflect.TypeOf(slice)) // [1, 3] []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] >> [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] []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
16 changes: 7 additions & 9 deletions arrayOperations.go
Expand Up @@ -4,16 +4,14 @@ import (
"reflect"
)

var tempVal reflect.Value

// Distinct returns the unique vals of a slice
//
// [1, 1, 2, 3] >> [1, 2, 3]
func Distinct(arr interface{}) (reflect.Value, bool) {
// create a slice from our input interface
slice, ok := takeArg(arr, reflect.Slice)
if !ok {
return tempVal, ok
return reflect.Value{}, ok
}

// put the values of our slice into a map
Expand Down Expand Up @@ -52,12 +50,12 @@ func Intersect(arrs ...interface{}) (reflect.Value, bool) {
for i, arg := range arrs {
tempArr, ok := Distinct(arg)
if !ok {
return tempVal, ok
return reflect.Value{}, ok
}

// check to be sure the type hasn't changed
if i > 0 && tempArr.Index(0).Kind() != kind {
return tempVal, false
return reflect.Value{}, false
}
kind = tempArr.Index(0).Kind()

Expand Down Expand Up @@ -107,12 +105,12 @@ func Union(arrs ...interface{}) (reflect.Value, bool) {
for i, arg := range arrs {
tempArr, ok := Distinct(arg)
if !ok {
return tempVal, ok
return reflect.Value{}, ok
}

// check to be sure the type hasn't changed
if i > 0 && tempArr.Index(0).Kind() != kind {
return tempVal, false
return reflect.Value{}, false
}
kind = tempArr.Index(0).Kind()

Expand Down Expand Up @@ -149,12 +147,12 @@ func Difference(arrs ...interface{}) (reflect.Value, bool) {
for i, arg := range arrs {
tempArr, ok := Distinct(arg)
if !ok {
return tempVal, ok
return reflect.Value{}, ok
}

// check to be sure the type hasn't changed
if i > 0 && tempArr.Index(0).Kind() != kind {
return tempVal, false
return reflect.Value{}, false
}
kind = tempArr.Index(0).Kind()

Expand Down

0 comments on commit 2936c12

Please sign in to comment.