diff --git a/some.go b/some.go new file mode 100644 index 0000000..ab1b869 --- /dev/null +++ b/some.go @@ -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 +} diff --git a/some_test.go b/some_test.go new file mode 100644 index 0000000..8082843 --- /dev/null +++ b/some_test.go @@ -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)) + +}