underscore/takewhile.go
Ruidy 482b8991df
feat: add TakeWhile and DropWhile functions
- Add TakeWhile: returns elements while predicate is true
- Add DropWhile: drops elements while predicate is true
- Comprehensive tests including edge cases
- Benchmarks included

Resolves Issue 14

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-14 14:47:53 +01:00

17 lines
465 B
Go

package underscore
// TakeWhile returns elements from the beginning of the slice while the predicate returns true.
// It stops at the first element where the predicate returns false.
func TakeWhile[T any](values []T, predicate func(T) bool) []T {
for i, v := range values {
if !predicate(v) {
res := make([]T, i)
copy(res, values[:i])
return res
}
}
// All elements satisfy predicate
res := make([]T, len(values))
copy(res, values)
return res
}