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

952 B

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

Replicate creates a slice containing count copies of value. Returns an empty slice if count is less than or equal to 0. Useful for initialization and testing.

package main

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

func main() {
	// Basic usage
	fmt.Println(u.Replicate(3, "hello"))
	// ["hello", "hello", "hello"]

	// Numbers
	fmt.Println(u.Replicate(5, 0))
	// [0, 0, 0, 0, 0]

	// Zero count
	fmt.Println(u.Replicate(0, 42))
	// []

	// Negative count
	fmt.Println(u.Replicate(-5, "x"))
	// []

	// Use case: initialize with default values
	defaultScores := u.Replicate(10, 100)
	fmt.Println(defaultScores)
	// [100, 100, 100, 100, 100, 100, 100, 100, 100, 100]

	// Use case: creating separators
	separator := u.Replicate(40, "-")
	fmt.Println(u.Reduce(separator, func(s, acc string) string { return acc + s }, ""))
	// ----------------------------------------
}