mirror of
https://github.com/rjNemo/underscore
synced 2026-06-09 03:56:49 +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>
43 lines
952 B
Markdown
43 lines
952 B
Markdown
---
|
|
title: "Replicate"
|
|
date: 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.
|
|
|
|
```go
|
|
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 }, ""))
|
|
// ----------------------------------------
|
|
}
|
|
```
|