underscore/docs/content/collections/intersperse.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

700 B

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

Intersperse inserts a separator between each element of the slice. Returns an empty slice if the input is empty. Returns the original element if the input has only one element.

package main

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

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

	// Useful for formatting
	words := []string{"apple", "banana", "cherry"}
	fmt.Println(u.Intersperse(words, ",")) // ["apple", ",", "banana", ",", "cherry"]

	// Single element - no separator added
	single := []int{42}
	fmt.Println(u.Intersperse(single, 0)) // [42]
}