From 6b377350d67b1fec04b7b67be012715525c9bcda Mon Sep 17 00:00:00 2001 From: Ruidy Date: Thu, 24 Mar 2022 12:30:01 -0400 Subject: [PATCH] U 21 diff (#23) * add logo and fix project image description * add difference function * update docs Co-authored-by: Ruidy --- README.md | 2 +- difference.go | 8 ++++++++ difference_test.go | 17 +++++++++++++++++ docs/content/collections/difference.md | 22 ++++++++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 difference.go create mode 100644 difference_test.go create mode 100644 docs/content/collections/difference.md diff --git a/README.md b/README.md index 48d5d15..fc2d66c 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ ![Go version](https://img.shields.io/github/go-mod/go-version/rjNemo/underscore?style=for-the-badge) ![test coverage](https://img.shields.io/codecov/c/github/rjNemo/underscore?style=for-the-badge) -![underscore](https://socialify.git.ci/rjNemo/underscore/image?description=1&descriptionEditable=Useful%20functional%20programming%20helpers%20for%20Go%201.18%20and%20beyond&font=Raleway&language=1&logo=https%3A%2F%2Fgithub.com%2FrjNemo%2Funderscore%2Fblob%2Fmain%2Fdocs%2Fstatic%2Flogo.png&owner=1&pattern=Floating%20Cogs&stargazers=1&theme=Light) +![underscore](https://socialify.git.ci/rjNemo/underscore/image?description=1&descriptionEditable=Useful%20functional%20programming%20helpers%20for%20Go%201.18%20and%20beyond&font=Raleway&language=1&logo=https%3A%2F%2Fraw.githubusercontent.com%2FrjNemo%2Funderscore%2Fmain%2Fdocs%2Fstatic%2Flogo.png&owner=1&pattern=Floating%20Cogs&stargazers=1&theme=Light) `underscore` is a `Go` library providing useful functional programming helpers without extending any built-in objects. diff --git a/difference.go b/difference.go new file mode 100644 index 0000000..d7d4d1a --- /dev/null +++ b/difference.go @@ -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) + }) +} diff --git a/difference_test.go b/difference_test.go new file mode 100644 index 0000000..795950d --- /dev/null +++ b/difference_test.go @@ -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)) +} diff --git a/docs/content/collections/difference.md b/docs/content/collections/difference.md new file mode 100644 index 0000000..680d0a1 --- /dev/null +++ b/docs/content/collections/difference.md @@ -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} +} +```