mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 02:26:42 +00:00
Updating Count function (#36)
* 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 * Updating count to work with any so you can count structs as well as basic types * Removing extra underscores
This commit is contained in:
parent
541e707861
commit
bfac048fb4
2 changed files with 4 additions and 3 deletions
2
count.go
2
count.go
|
|
@ -2,7 +2,7 @@ package underscore
|
||||||
|
|
||||||
// Count returns the number of elements in the slice that satisfy the predicate.
|
// 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
|
// 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 {
|
func Count[T any](slice []T, predicate func(T) bool) int {
|
||||||
count := 0
|
count := 0
|
||||||
for _, item := range slice {
|
for _, item := range slice {
|
||||||
if predicate(item) {
|
if predicate(item) {
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
package underscore
|
package underscore
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_Count_Can_Count_Numbers(t *testing.T) {
|
func Test_Count_Can_Count_Numbers(t *testing.T) {
|
||||||
|
|
@ -21,7 +22,7 @@ type People struct {
|
||||||
Gender string
|
Gender string
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_Count_Can_Count__People(t *testing.T) {
|
func Test_Count_Can_Count_People(t *testing.T) {
|
||||||
people := []People{
|
people := []People{
|
||||||
{Name: "Andy", Age: 43, Gender: "M"},
|
{Name: "Andy", Age: 43, Gender: "M"},
|
||||||
{Name: "Fred", Age: 33, Gender: "M"},
|
{Name: "Fred", Age: 33, Gender: "M"},
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue