underscore/docs/content/collections/init.md
Ruidy bcb4dd1e9d
docs: add documentation for new collection functions
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>
2025-11-16 08:48:38 +01:00

670 B

title date
Init 2025-01-16T00:00:00-00:00

Init returns all elements except the last one, and the last element separately. Returns an empty slice and zero value if the input slice is empty. Useful for destructuring lists from the right.

package main

import (
	"fmt"
	u "github.com/rjNemo/underscore"
)

func main() {
	nums := []int{1, 2, 3, 4, 5}
	init, last := u.Init(nums)
	fmt.Println(init) // [1, 2, 3, 4]
	fmt.Println(last) // 5

	// Single element
	single, val := u.Init([]int{42})
	fmt.Println(single) // []
	fmt.Println(val)    // 42

	// Empty slice
	empty, zero := u.Init([]int{})
	fmt.Println(empty) // []
	fmt.Println(zero)  // 0
}