mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 02:26:42 +00:00
feat: add Init function (all but last)
- Add Init: returns all elements except last, and the last element - Useful for destructuring lists from the right - Comprehensive tests including edge cases - Benchmark included Example: Init([1,2,3,4,5]) → ([1,2,3,4], 5) Resolves Issue 17 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
d622c8cba8
commit
3df26bb95c
2 changed files with 83 additions and 0 deletions
18
init.go
Normal file
18
init.go
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package underscore
|
||||
|
||||
// Init returns all elements except the last one, and the last element separately.
|
||||
// Returns an empty slice and zero value if the input slice is empty.
|
||||
// Also known as "uncons from the right" or "snoc" inverse.
|
||||
func Init[T any](values []T) ([]T, T) {
|
||||
var last T
|
||||
if len(values) == 0 {
|
||||
return []T{}, last
|
||||
}
|
||||
if len(values) == 1 {
|
||||
return []T{}, values[0]
|
||||
}
|
||||
|
||||
res := make([]T, len(values)-1)
|
||||
copy(res, values[:len(values)-1])
|
||||
return res, values[len(values)-1]
|
||||
}
|
||||
65
init_test.go
Normal file
65
init_test.go
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
package underscore_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
u "github.com/rjNemo/underscore"
|
||||
)
|
||||
|
||||
func TestInit(t *testing.T) {
|
||||
nums := []int{1, 2, 3, 4, 5}
|
||||
init, last := u.Init(nums)
|
||||
assert.Equal(t, []int{1, 2, 3, 4}, init)
|
||||
assert.Equal(t, 5, last)
|
||||
}
|
||||
|
||||
func TestInitEmpty(t *testing.T) {
|
||||
init, last := u.Init([]int{})
|
||||
assert.Equal(t, []int{}, init)
|
||||
assert.Equal(t, 0, last)
|
||||
}
|
||||
|
||||
func TestInitSingleElement(t *testing.T) {
|
||||
init, last := u.Init([]int{42})
|
||||
assert.Equal(t, []int{}, init)
|
||||
assert.Equal(t, 42, last)
|
||||
}
|
||||
|
||||
func TestInitTwoElements(t *testing.T) {
|
||||
init, last := u.Init([]int{1, 2})
|
||||
assert.Equal(t, []int{1}, init)
|
||||
assert.Equal(t, 2, last)
|
||||
}
|
||||
|
||||
func TestInitStrings(t *testing.T) {
|
||||
words := []string{"hello", "world", "!"}
|
||||
init, last := u.Init(words)
|
||||
assert.Equal(t, []string{"hello", "world"}, init)
|
||||
assert.Equal(t, "!", last)
|
||||
}
|
||||
|
||||
func TestInitDoesNotMutate(t *testing.T) {
|
||||
original := []int{1, 2, 3, 4, 5}
|
||||
init, last := u.Init(original)
|
||||
|
||||
// Modify returned slice
|
||||
init[0] = 999
|
||||
|
||||
// Original should be unchanged
|
||||
assert.Equal(t, 1, original[0])
|
||||
assert.Equal(t, 5, last)
|
||||
}
|
||||
|
||||
func BenchmarkInit(b *testing.B) {
|
||||
nums := make([]int, 1000)
|
||||
for i := range nums {
|
||||
nums[i] = i
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
u.Init(nums)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue