mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 02:26:42 +00:00
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>
28 lines
700 B
Markdown
28 lines
700 B
Markdown
---
|
|
title: "Intersperse"
|
|
date: 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.
|
|
|
|
```go
|
|
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]
|
|
}
|
|
```
|