underscore/docs/content/collections/replicate.md
Ruidy 3617c2de8f
docs: update Last documentation to reflect panic behavior
Document that Last panics on empty slices with a clear error message.
Add examples for single element and empty slice cases.

Related to Issue 13 (PR #41)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-16 08:51:36 +01:00

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 }, ""))
// ----------------------------------------
}
```