mirror of
https://github.com/rjNemo/underscore
synced 2026-06-09 20:16:44 +00:00
Add comprehensive documentation for all new functions: - TakeWhile: take elements while predicate is true - DropWhile: drop elements while predicate is true - Scan: running accumulator (prefix scan) - First/FirstN: get first element(s) safely - Init: all but last element - Intersperse: insert separator between elements - Sliding: sliding window views - FoldRight: right-to-left fold/reduce - Tap: side effects without mutation - Transpose: flip matrix rows/columns - Unzip: split tuples into separate slices - ParallelReduce: parallel reduction (experimental) - Replicate: create n copies of a value Each doc includes: - Clear description - Code examples with output - Common use cases - Edge case handling 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
23 lines
555 B
Markdown
23 lines
555 B
Markdown
---
|
|
title: "FirstN"
|
|
date: 2025-01-16T00:00:00-00:00
|
|
---
|
|
|
|
`FirstN` returns the first n elements of the slice. If n is greater than the slice length, returns the entire slice. If n is less than or equal to 0, returns an empty slice.
|
|
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
u "github.com/rjNemo/underscore"
|
|
)
|
|
|
|
func main() {
|
|
nums := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
|
|
fmt.Println(u.FirstN(nums, 3)) // [1, 2, 3]
|
|
fmt.Println(u.FirstN(nums, 0)) // []
|
|
fmt.Println(u.FirstN(nums, 10)) // [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
fmt.Println(u.FirstN(nums, -5)) // []
|
|
}
|
|
```
|