mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 02:26:42 +00:00
feat: some
This commit is contained in:
parent
9a96936354
commit
3834edefb4
2 changed files with 29 additions and 0 deletions
12
some.go
Normal file
12
some.go
Normal 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
17
some_test.go
Normal 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))
|
||||
|
||||
}
|
||||
Loading…
Reference in a new issue