mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 02:26:42 +00:00
U 21 diff (#23)
* add logo and fix project image description * add difference function * update docs Co-authored-by: Ruidy <rnemausat@newstore.com>
This commit is contained in:
parent
eebeb65114
commit
6b377350d6
4 changed files with 48 additions and 1 deletions
|
|
@ -4,7 +4,7 @@
|
||||||

|

|
||||||

|

|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
`underscore` is a `Go` library providing useful functional programming helpers without extending any built-in objects.
|
`underscore` is a `Go` library providing useful functional programming helpers without extending any built-in objects.
|
||||||
|
|
||||||
|
|
|
||||||
8
difference.go
Normal file
8
difference.go
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
package underscore
|
||||||
|
|
||||||
|
// Difference Returns a copy of the array with all instances of the values that are not present in the other array.
|
||||||
|
func Difference[T comparable](slice, other []T) []T {
|
||||||
|
return Filter(slice, func(n T) bool {
|
||||||
|
return !Contains(other, n)
|
||||||
|
})
|
||||||
|
}
|
||||||
17
difference_test.go
Normal file
17
difference_test.go
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
package underscore_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
u "github.com/rjNemo/underscore"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDifference(t *testing.T) {
|
||||||
|
nums := []int{1, 3, 5, 6, 7, 9}
|
||||||
|
reject := []int{9, 7, 5, 4}
|
||||||
|
want := []int{1, 3, 6}
|
||||||
|
|
||||||
|
assert.Equal(t, want, u.Difference(nums, reject))
|
||||||
|
}
|
||||||
22
docs/content/collections/difference.md
Normal file
22
docs/content/collections/difference.md
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
---
|
||||||
|
title: "Difference"
|
||||||
|
date: 2022-03-21T13:48:21-04:00
|
||||||
|
---
|
||||||
|
|
||||||
|
Returns a copy of the array with all instances of the values that are not present in the other arrays.
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
u "github.com/rjNemo/underscore"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
nums := []int{1, 3, 5, 6, 7, 9}
|
||||||
|
reject := []int{9, 7, 5, 4}
|
||||||
|
|
||||||
|
fmt.Println(u.Difference(nums, reject)) // {1, 3, 6}
|
||||||
|
}
|
||||||
|
```
|
||||||
Loading…
Reference in a new issue