Skip to content

Commit 291df4f

Browse files
authored
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
1 parent 0bc3a54 commit 291df4f

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

count.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package underscore
2+
3+
// Count returns the number of elements in the slice that satisfy the predicate.
4+
// example: Count([]int{1,2,3,4,5}, func(n int) bool { return n%2 == 0 }) // 2
5+
func Count[T comparable](slice []T, predicate func(T) bool) int {
6+
count := 0
7+
for _, item := range slice {
8+
if predicate(item) {
9+
count++
10+
}
11+
}
12+
return count
13+
}

count_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package underscore
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func Test_Count_Can_Count_Numbers(t *testing.T) {
10+
numbers := Range(1, 100)
11+
count := Count(numbers, func(n int) bool {
12+
return n%2 == 0
13+
})
14+
15+
assert.Equal(t, 50, count)
16+
}
17+
18+
type People struct {
19+
Name string
20+
Age int
21+
Gender string
22+
}
23+
24+
func Test_Count_Can_Count__People(t *testing.T) {
25+
people := []People{
26+
{Name: "Andy", Age: 43, Gender: "M"},
27+
{Name: "Fred", Age: 33, Gender: "M"},
28+
{Name: "Jack", Age: 23, Gender: "M"},
29+
{Name: "Jill", Age: 43, Gender: "F"},
30+
{Name: "Anna", Age: 33, Gender: "F"},
31+
{Name: "Arya", Age: 23, Gender: "F"},
32+
{Name: "Jane", Age: 13, Gender: "F"},
33+
}
34+
35+
a := Count(people, func(p People) bool {
36+
return strings.HasPrefix(p.Name, "A")
37+
})
38+
assert.Equal(t, 3, a)
39+
40+
females := Count(people, func(p People) bool {
41+
return p.Gender == "F"
42+
})
43+
assert.Equal(t, 4, females)
44+
45+
males := Count(people, func(p People) bool {
46+
return p.Gender == "M"
47+
})
48+
assert.Equal(t, 3, males)
49+
50+
over30 := Count(people, func(p People) bool {
51+
return p.Age > 30
52+
})
53+
assert.Equal(t, 4, over30)
54+
55+
under30 := Count(people, func(p People) bool {
56+
return p.Age < 30
57+
})
58+
assert.Equal(t, 3, under30)
59+
60+
under20 := Count(people, func(p People) bool {
61+
return p.Age < 20
62+
})
63+
assert.Equal(t, 1, under20)
64+
}

0 commit comments

Comments
 (0)