Commit b691eda

mo khan <mo@mokhan.ca>
2025-04-30 18:54:55
feat: add Prepend[T] function to add items to the beginning of a slice
1 parent 4b27adc
Changed files (2)
pkg/x/iterate.go
@@ -47,3 +47,7 @@ func Inject[TInput any, TOutput any](items []TInput, memo TOutput, f func(TOutpu
 	}
 	return memo
 }
+
+func Prepend[T any](rest []T, beginning ...T) []T {
+	return append(beginning, rest...)
+}
pkg/x/iterate_test.go
@@ -133,3 +133,23 @@ func TestInject(t *testing.T) {
 		assert.True(t, result[6])
 	})
 }
+
+func TestPrepend(t *testing.T) {
+	t.Run("adds a new item to the beginning", func(t *testing.T) {
+		items := []int{1, 3, 6}
+
+		result := Prepend[int](items, 7)
+
+		require.NotZero(t, result)
+		assert.Equal(t, []int{7, 1, 3, 6}, result)
+	})
+
+	t.Run("adss multiple items to the beginning", func(t *testing.T) {
+		items := []int{1, 3, 6}
+
+		result := Prepend[int](items, 7, 9)
+
+		require.NotZero(t, result)
+		assert.Equal(t, []int{7, 9, 1, 3, 6}, result)
+	})
+}