mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 02:26:42 +00:00
Document that Last panics on empty slices with a clear error message. Add examples for single element and empty slice cases. Related to Issue 13 (PR #41) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
25 lines
635 B
Markdown
25 lines
635 B
Markdown
---
|
|
title: "TakeWhile"
|
|
date: 2025-01-16T00:00:00-00:00
|
|
---
|
|
|
|
`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.
|
|
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
u "github.com/rjNemo/underscore"
|
|
)
|
|
|
|
func main() {
|
|
nums := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
|
|
lessThan5 := func(n int) bool { return n < 5 }
|
|
fmt.Println(u.TakeWhile(nums, lessThan5)) // [1, 2, 3, 4]
|
|
|
|
words := []string{"apple", "banana", "cherry", "date"}
|
|
shortWords := func(s string) bool { return len(s) < 6 }
|
|
fmt.Println(u.TakeWhile(words, shortWords)) // ["apple"]
|
|
}
|
|
```
|