Skip to content

Commit 260c48e

Browse files
rjNemoclaude
andauthored
feat: add Init function (all but last) (#45)
- 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>
1 parent 5240c27 commit 260c48e

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

init.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package underscore
2+
3+
// Init returns all elements except the last one, and the last element separately.
4+
// Returns an empty slice and zero value if the input slice is empty.
5+
// Also known as "uncons from the right" or "snoc" inverse.
6+
func Init[T any](values []T) ([]T, T) {
7+
var last T
8+
if len(values) == 0 {
9+
return []T{}, last
10+
}
11+
if len(values) == 1 {
12+
return []T{}, values[0]
13+
}
14+
15+
res := make([]T, len(values)-1)
16+
copy(res, values[:len(values)-1])
17+
return res, values[len(values)-1]
18+
}

init_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package underscore_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
u "github.com/rjNemo/underscore"
9+
)
10+
11+
func TestInit(t *testing.T) {
12+
nums := []int{1, 2, 3, 4, 5}
13+
init, last := u.Init(nums)
14+
assert.Equal(t, []int{1, 2, 3, 4}, init)
15+
assert.Equal(t, 5, last)
16+
}
17+
18+
func TestInitEmpty(t *testing.T) {
19+
init, last := u.Init([]int{})
20+
assert.Equal(t, []int{}, init)
21+
assert.Equal(t, 0, last)
22+
}
23+
24+
func TestInitSingleElement(t *testing.T) {
25+
init, last := u.Init([]int{42})
26+
assert.Equal(t, []int{}, init)
27+
assert.Equal(t, 42, last)
28+
}
29+
30+
func TestInitTwoElements(t *testing.T) {
31+
init, last := u.Init([]int{1, 2})
32+
assert.Equal(t, []int{1}, init)
33+
assert.Equal(t, 2, last)
34+
}
35+
36+
func TestInitStrings(t *testing.T) {
37+
words := []string{"hello", "world", "!"}
38+
init, last := u.Init(words)
39+
assert.Equal(t, []string{"hello", "world"}, init)
40+
assert.Equal(t, "!", last)
41+
}
42+
43+
func TestInitDoesNotMutate(t *testing.T) {
44+
original := []int{1, 2, 3, 4, 5}
45+
init, last := u.Init(original)
46+
47+
// Modify returned slice
48+
init[0] = 999
49+
50+
// Original should be unchanged
51+
assert.Equal(t, 1, original[0])
52+
assert.Equal(t, 5, last)
53+
}
54+
55+
func BenchmarkInit(b *testing.B) {
56+
nums := make([]int, 1000)
57+
for i := range nums {
58+
nums[i] = i
59+
}
60+
61+
b.ResetTimer()
62+
for i := 0; i < b.N; i++ {
63+
u.Init(nums)
64+
}
65+
}

0 commit comments

Comments
 (0)