From 3df26bb95cdaad778ec99dc4439bc4a35d11a2f0 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Fri, 14 Nov 2025 14:50:45 +0100 Subject: [PATCH] feat: add Init function (all but last) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- init.go | 18 +++++++++++++++ init_test.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 init.go create mode 100644 init_test.go diff --git a/init.go b/init.go new file mode 100644 index 0000000..201e062 --- /dev/null +++ b/init.go @@ -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] +} diff --git a/init_test.go b/init_test.go new file mode 100644 index 0000000..c0ba786 --- /dev/null +++ b/init_test.go @@ -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) + } +}