mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 02:26:42 +00:00
feat: add Intersperse function
- Add Intersperse: inserts separator between each element - Pre-allocated for optimal performance - Comprehensive tests including edge cases - Benchmark included Example: Intersperse([1,2,3], 0) → [1,0,2,0,3] Resolves Issue 18 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
d622c8cba8
commit
0ca15a80c1
2 changed files with 83 additions and 0 deletions
23
intersperse.go
Normal file
23
intersperse.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package underscore
|
||||
|
||||
// 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.
|
||||
//
|
||||
// Example: Intersperse([]int{1,2,3}, 0) → [1, 0, 2, 0, 3]
|
||||
func Intersperse[T any](values []T, separator T) []T {
|
||||
if len(values) == 0 {
|
||||
return []T{}
|
||||
}
|
||||
if len(values) == 1 {
|
||||
return []T{values[0]}
|
||||
}
|
||||
|
||||
// Result will have len(values) + (len(values)-1) elements
|
||||
res := make([]T, 0, len(values)*2-1)
|
||||
res = append(res, values[0])
|
||||
for i := 1; i < len(values); i++ {
|
||||
res = append(res, separator, values[i])
|
||||
}
|
||||
return res
|
||||
}
|
||||
60
intersperse_test.go
Normal file
60
intersperse_test.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package underscore_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
u "github.com/rjNemo/underscore"
|
||||
)
|
||||
|
||||
func TestIntersperse(t *testing.T) {
|
||||
nums := []int{1, 2, 3, 4, 5}
|
||||
result := u.Intersperse(nums, 0)
|
||||
assert.Equal(t, []int{1, 0, 2, 0, 3, 0, 4, 0, 5}, result)
|
||||
}
|
||||
|
||||
func TestIntersperseEmpty(t *testing.T) {
|
||||
result := u.Intersperse([]int{}, 0)
|
||||
assert.Equal(t, []int{}, result)
|
||||
}
|
||||
|
||||
func TestIntersperseSingleElement(t *testing.T) {
|
||||
result := u.Intersperse([]int{42}, 0)
|
||||
assert.Equal(t, []int{42}, result)
|
||||
}
|
||||
|
||||
func TestIntersperseTwoElements(t *testing.T) {
|
||||
result := u.Intersperse([]int{1, 2}, 0)
|
||||
assert.Equal(t, []int{1, 0, 2}, result)
|
||||
}
|
||||
|
||||
func TestIntersperseStrings(t *testing.T) {
|
||||
words := []string{"hello", "world", "!"}
|
||||
result := u.Intersperse(words, ",")
|
||||
assert.Equal(t, []string{"hello", ",", "world", ",", "!"}, result)
|
||||
}
|
||||
|
||||
func TestIntersperseComma(t *testing.T) {
|
||||
words := []string{"apple", "banana", "cherry"}
|
||||
result := u.Intersperse(words, ",")
|
||||
assert.Equal(t, []string{"apple", ",", "banana", ",", "cherry"}, result)
|
||||
}
|
||||
|
||||
func TestIntersperseNegativeNumber(t *testing.T) {
|
||||
nums := []int{1, 2, 3}
|
||||
result := u.Intersperse(nums, -1)
|
||||
assert.Equal(t, []int{1, -1, 2, -1, 3}, result)
|
||||
}
|
||||
|
||||
func BenchmarkIntersperse(b *testing.B) {
|
||||
nums := make([]int, 100)
|
||||
for i := range nums {
|
||||
nums[i] = i
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
u.Intersperse(nums, 0)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue