From 291df4fe4e06e5ea4f29a612af7c6531e9e004bb Mon Sep 17 00:00:00 2001 From: Andy Long Date: Wed, 30 Nov 2022 12:34:01 +0000 Subject: [PATCH 1/4] Adding a Count function (#34) * Adding some new funky functions which I find useful Created a Tuple struct as some of the new functions require you to return a new slice with two fields which is the result of the new functions Created the Join, JoinProjection, Range, SumMap, Zip functions, ecah fuction is documented with how it works and had a unit test or maybe more * Added in an OrderBy function * Documentation comment for OrderBy which I missed out * Adding a Unit test for JoinProject function Updated the comments on the Join & OrderBy functions so they make a little more sense. Covered an extra test case with the Join test, where the left set has more data than the right and so the Right handside array of the join is empty * Adding a count method to the package, so you can find out how many items in a slice satisfy and given condition --- count.go | 13 +++++++++++ count_test.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 count.go create mode 100644 count_test.go diff --git a/count.go b/count.go new file mode 100644 index 0000000..581c053 --- /dev/null +++ b/count.go @@ -0,0 +1,13 @@ +package underscore + +// Count returns the number of elements in the slice that satisfy the predicate. +// example: Count([]int{1,2,3,4,5}, func(n int) bool { return n%2 == 0 }) // 2 +func Count[T comparable](slice []T, predicate func(T) bool) int { + count := 0 + for _, item := range slice { + if predicate(item) { + count++ + } + } + return count +} diff --git a/count_test.go b/count_test.go new file mode 100644 index 0000000..9a876ef --- /dev/null +++ b/count_test.go @@ -0,0 +1,64 @@ +package underscore + +import ( + "github.com/stretchr/testify/assert" + "strings" + "testing" +) + +func Test_Count_Can_Count_Numbers(t *testing.T) { + numbers := Range(1, 100) + count := Count(numbers, func(n int) bool { + return n%2 == 0 + }) + + assert.Equal(t, 50, count) +} + +type People struct { + Name string + Age int + Gender string +} + +func Test_Count_Can_Count__People(t *testing.T) { + people := []People{ + {Name: "Andy", Age: 43, Gender: "M"}, + {Name: "Fred", Age: 33, Gender: "M"}, + {Name: "Jack", Age: 23, Gender: "M"}, + {Name: "Jill", Age: 43, Gender: "F"}, + {Name: "Anna", Age: 33, Gender: "F"}, + {Name: "Arya", Age: 23, Gender: "F"}, + {Name: "Jane", Age: 13, Gender: "F"}, + } + + a := Count(people, func(p People) bool { + return strings.HasPrefix(p.Name, "A") + }) + assert.Equal(t, 3, a) + + females := Count(people, func(p People) bool { + return p.Gender == "F" + }) + assert.Equal(t, 4, females) + + males := Count(people, func(p People) bool { + return p.Gender == "M" + }) + assert.Equal(t, 3, males) + + over30 := Count(people, func(p People) bool { + return p.Age > 30 + }) + assert.Equal(t, 4, over30) + + under30 := Count(people, func(p People) bool { + return p.Age < 30 + }) + assert.Equal(t, 3, under30) + + under20 := Count(people, func(p People) bool { + return p.Age < 20 + }) + assert.Equal(t, 1, under20) +} From 054679fb1816f4e15e48de14079ebef484683a8b Mon Sep 17 00:00:00 2001 From: Ruidy Date: Wed, 7 Jun 2023 14:49:55 +0200 Subject: [PATCH 2/4] add group by to docs --- Makefile | 1 + docs/content/_index.md | 3 +-- docs/content/collections/groupby.md | 21 +++++++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 docs/content/collections/groupby.md diff --git a/Makefile b/Makefile index 074cbde..5baa33d 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,7 @@ scan: scan-config: trivy config . +.PHONY: docs docs: cd docs && hugo server -D diff --git a/docs/content/_index.md b/docs/content/_index.md index f717fe2..cbaaacb 100644 --- a/docs/content/_index.md +++ b/docs/content/_index.md @@ -9,10 +9,9 @@ title: _Underscore ![underscore](https://socialify.git.ci/rjNemo/underscore/image?description=1&font=KoHo&language=1&logo=https%3A%2F%2Fraw.githubusercontent.com%2FrjNemo%2Funderscore%2Fmain%2Fdocs%2Fstatic%2Flogo.png&owner=1&pattern=Floating%20Cogs&stargazers=1&theme=Dark) - `underscore` is a `Go` library providing useful functional programming helpers without extending any built-in objects. -It is mostly a port from the `underscore.js` library based on generics brought by `go1.18`. +It is mostly a port from the `underscore.js` library based on generics available from `go1.18`. ## Quick Start diff --git a/docs/content/collections/groupby.md b/docs/content/collections/groupby.md new file mode 100644 index 0000000..04cb548 --- /dev/null +++ b/docs/content/collections/groupby.md @@ -0,0 +1,21 @@ +--- +title: "Group by" +date: 2023-06-07T00:49:56+02:00 +--- + +GroupBy splits a slice into a map[K][]V grouped by the result of the iterator function. + +```go +package main + +import ( + "fmt" + u "github.com/rjNemo/underscore" +) + +func main() { + nums := []float64{1.3, 2.1, 2.4} + groupingFunc := func(n float64) int { return int(math.Floor(n)) } + res := u.GroupBy(nums, groupingFunc) // { 1: {1.3}, 2: {2.1, 2.4}} +} +``` From 64fcfee748b845897dca3f6949ea60e7b9ad66b0 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Wed, 7 Jun 2023 14:55:06 +0200 Subject: [PATCH 3/4] update go version --- go.mod | 8 ++++---- go.sum | 15 ++++++--------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index 371f6eb..a75f661 100644 --- a/go.mod +++ b/go.mod @@ -1,14 +1,14 @@ module github.com/rjNemo/underscore -go 1.18 +go 1.20 require ( - github.com/stretchr/testify v1.7.0 - golang.org/x/exp v0.0.0-20220314205449-43aec2f8a4e7 + github.com/stretchr/testify v1.8.4 + golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 99f8a6a..925e02e 100644 --- a/go.sum +++ b/go.sum @@ -1,15 +1,12 @@ -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -golang.org/x/exp v0.0.0-20220314205449-43aec2f8a4e7 h1:jynE66seADJbyWMUdeOyVTvPtBZt7L6LJHupGwxPZRM= -golang.org/x/exp v0.0.0-20220314205449-43aec2f8a4e7/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc= +golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From 541e707861ada911b663ed53130a46f07de5f2da Mon Sep 17 00:00:00 2001 From: Ruidy Date: Wed, 7 Jun 2023 15:11:21 +0200 Subject: [PATCH 4/4] Update unit-test.yml --- .github/workflows/unit-test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml index ea15f39..42cd86d 100644 --- a/.github/workflows/unit-test.yml +++ b/.github/workflows/unit-test.yml @@ -10,7 +10,7 @@ jobs: fetch-depth: 2 - uses: actions/setup-go@v2 with: - go-version: '1.18' + go-version: '1.20' - name: Run tests with coverage run: go test -coverprofile=coverage.out -covermode=count ./... - uses: codecov/codecov-action@v2 @@ -20,4 +20,4 @@ jobs: flags: unittests name: codecov-umbrella fail_ci_if_error: true - verbose: true \ No newline at end of file + verbose: true