Skip to content

Commit

Permalink
fix(substraction): preserve order and duplication (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
samber authored Mar 9, 2020
1 parent 5834514 commit 8c9292a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
15 changes: 15 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,21 @@ see also, typesafe implementations: ShuffleInt_, ShuffleInt64_, ShuffleFloat32_,
.. _ShuffleInt64: https://godoc.org/github.com/thoas/go-funk#ShuffleInt64
.. _ShuffleString: https://godoc.org/github.com/thoas/go-funk#ShuffleString

funk.Subtract
............

Returns the subtraction between two collections. It preserve order.

.. code-block:: go
funk.Subtract([]int{0, 1, 2, 3, 4}, []int{0, 4}) // []int{1, 2, 3}
funk.Subtract([]int{0, 3, 2, 3, 4}, []int{0, 4}) // []int{3, 2, 3}
see also, typesafe implementations: SubtractString_

.. SubtractString: https://godoc.org/github.com/thoas/go-funk#SubtractString
funk.Sum
........

Expand Down
12 changes: 12 additions & 0 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ func sliceGenerator(size uint, r *rand.Rand) (out []int64) {
return
}

func BenchmarkSubtract(b *testing.B) {
r := rand.New(rand.NewSource(seed))
testData := sliceGenerator(sliceSize, r)
what := sliceGenerator(sliceSize, r)

b.Run("Subtract", func(b *testing.B) {
for n := 0; n < b.N; n++ {
Subtract(testData, what)
}
})
}

func BenchmarkContains(b *testing.B) {
r := rand.New(rand.NewSource(seed))
testData := sliceGenerator(sliceSize, r)
Expand Down
20 changes: 13 additions & 7 deletions subtraction.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ func Subtract(x interface{}, y interface{}) interface{} {
}
}

for k, _ := range hash {
kValue := reflect.ValueOf(k)
zSlice = reflect.Append(zSlice, kValue)
for i := 0; i < xValue.Len(); i++ {
v := xValue.Index(i).Interface()
_, ok := hash[v]
if ok {
zSlice = reflect.Append(zSlice, xValue.Index(i))
}
}

return zSlice.Interface()
Expand All @@ -55,7 +58,7 @@ func SubtractString(x []string, y []string) []string {
return []string{}
}

set := []string{}
slice := []string{}
hash := map[string]struct{}{}

for _, v := range x {
Expand All @@ -69,9 +72,12 @@ func SubtractString(x []string, y []string) []string {
}
}

for k, _ := range hash {
set = append(set, k)
for _, v := range x {
_, ok := hash[v]
if ok {
slice = append(slice, v)
}
}

return set
return slice
}
6 changes: 6 additions & 0 deletions subtraction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@ func TestSubtract(t *testing.T) {

r = Subtract([]string{"foo", "bar", "hello", "bar", "hi"}, []string{"foo", "bar"})
is.Equal([]string{"hello", "hi"}, r)

r = Subtract([]string{"hello", "foo", "bar", "hello", "bar", "hi"}, []string{"foo", "bar"})
is.Equal([]string{"hello", "hello", "hi"}, r)
}

func TestSubtractString(t *testing.T) {
is := assert.New(t)

r := SubtractString([]string{"foo", "bar", "hello", "bar"}, []string{"foo", "bar"})
is.Equal([]string{"hello"}, r)

r = SubtractString([]string{"foo", "bar", "hello", "bar", "world", "world"}, []string{"foo", "bar"})
is.Equal([]string{"hello", "world", "world"}, r)
}

0 comments on commit 8c9292a

Please sign in to comment.