mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 02:26:42 +00:00
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
This commit is contained in:
parent
0bc3a54efd
commit
291df4fe4e
2 changed files with 77 additions and 0 deletions
13
count.go
Normal file
13
count.go
Normal file
|
|
@ -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
|
||||||
|
}
|
||||||
64
count_test.go
Normal file
64
count_test.go
Normal file
|
|
@ -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)
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue