feat: every

This commit is contained in:
Ruidy 2021-12-29 13:16:26 -04:00
parent dd6d077c36
commit 4c7f3f7868
2 changed files with 27 additions and 0 deletions

12
every.go Normal file
View file

@ -0,0 +1,12 @@
package underscore
// Every returns true if all the values in the slice pass the predicate truth test.
// Short-circuits and stops traversing the slice if a false element is found.
func Every[T any](values []T, predicate func(T) bool) bool {
for _, v := range values {
if !predicate(v) {
return false
}
}
return true
}

15
every_test.go Normal file
View file

@ -0,0 +1,15 @@
package underscore_test
import (
"testing"
"github.com/stretchr/testify/assert"
u "github.com/rjNemo/underscore"
)
func TestEvery(t *testing.T) {
nums := []int{1, 3, 5, 7, 9}
isOdd := func(n int) bool { return n%2 != 0 }
assert.True(t, u.Every(nums, isOdd))
}