feat: some

This commit is contained in:
Ruidy 2021-12-29 10:48:41 -04:00
parent 9a96936354
commit 3834edefb4
2 changed files with 29 additions and 0 deletions

12
some.go Normal file
View file

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

17
some_test.go Normal file
View file

@ -0,0 +1,17 @@
package underscore_test
import (
"testing"
"github.com/stretchr/testify/assert"
u "github.com/rjNemo/underscore"
)
func TestSome(t *testing.T) {
nums := []int{1, 2, 4, 6, 8}
isEven := func(n int) bool { return n%2 == 0 }
assert.True(t, u.Some(nums, isEven))
}