From 291df4fe4e06e5ea4f29a612af7c6531e9e004bb Mon Sep 17 00:00:00 2001 From: Andy Long Date: Wed, 30 Nov 2022 12:34:01 +0000 Subject: [PATCH] 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) +}