From 6ceaadc0974377cc0ec86bcce9b70e3546c12fc3 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Fri, 14 Nov 2025 14:54:00 +0100 Subject: [PATCH] feat: add FoldRight function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add FoldRight: fold/reduce from right to left - Useful for non-associative operations - Comprehensive tests including comparison with Reduce - Benchmark included Example: FoldRight([1,2,3], 0, subtract) → 1-(2-(3-0)) = 2 Resolves Issue 20 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- foldright.go | 13 ++++++++ foldright_test.go | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 foldright.go create mode 100644 foldright_test.go diff --git a/foldright.go b/foldright.go new file mode 100644 index 0000000..cc501da --- /dev/null +++ b/foldright.go @@ -0,0 +1,13 @@ +package underscore + +// FoldRight is like Reduce but processes elements from right to left. +// Also known as foldr in Haskell. +// +// Example: FoldRight([]int{1,2,3}, 0, func(n, acc int) int { return n - acc }) +// → 1 - (2 - (3 - 0)) = 1 - (2 - 3) = 1 - (-1) = 2 +func FoldRight[T, P any](values []T, acc P, fn func(T, P) P) P { + for i := len(values) - 1; i >= 0; i-- { + acc = fn(values[i], acc) + } + return acc +} diff --git a/foldright_test.go b/foldright_test.go new file mode 100644 index 0000000..b92c008 --- /dev/null +++ b/foldright_test.go @@ -0,0 +1,80 @@ +package underscore_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + u "github.com/rjNemo/underscore" +) + +func TestFoldRight(t *testing.T) { + nums := []int{1, 2, 3, 4} + result := u.FoldRight(nums, 0, func(n, acc int) int { return n + acc }) + assert.Equal(t, 10, result) +} + +func TestFoldRightEmpty(t *testing.T) { + result := u.FoldRight([]int{}, 42, func(n, acc int) int { return n + acc }) + assert.Equal(t, 42, result) +} + +func TestFoldRightSingleElement(t *testing.T) { + result := u.FoldRight([]int{5}, 0, func(n, acc int) int { return n + acc }) + assert.Equal(t, 5, result) +} + +func TestFoldRightSubtraction(t *testing.T) { + // FoldRight: 1 - (2 - (3 - 0)) = 1 - (2 - 3) = 1 - (-1) = 2 + nums := []int{1, 2, 3} + result := u.FoldRight(nums, 0, func(n, acc int) int { return n - acc }) + assert.Equal(t, 2, result) +} + +func TestFoldRightDivision(t *testing.T) { + // FoldRight with float: 2.0 / (4.0 / (8.0 / 1.0)) = 2.0 / (4.0 / 8.0) = 2.0 / 0.5 = 4.0 + nums := []float64{2.0, 4.0, 8.0} + result := u.FoldRight(nums, 1.0, func(n, acc float64) float64 { return n / acc }) + assert.Equal(t, 4.0, result) +} + +func TestFoldRightStrings(t *testing.T) { + words := []string{"a", "b", "c"} + result := u.FoldRight(words, "", func(s, acc string) string { return s + acc }) + assert.Equal(t, "abc", result) +} + +func TestFoldRightVsReduce(t *testing.T) { + nums := []int{1, 2, 3} + + // Reduce (left fold): (0 - 1) - 2 - 3 = -6 + reduceResult := u.Reduce(nums, func(n, acc int) int { return acc - n }, 0) + assert.Equal(t, -6, reduceResult) + + // FoldRight: 1 - (2 - (3 - 0)) = 1 - (2 - 3) = 1 - (-1) = 2 + foldRightResult := u.FoldRight(nums, 0, func(n, acc int) int { return n - acc }) + assert.Equal(t, 2, foldRightResult) + + // They should be different for non-associative operations + assert.NotEqual(t, reduceResult, foldRightResult) +} + +func TestFoldRightBuildList(t *testing.T) { + nums := []int{1, 2, 3} + result := u.FoldRight(nums, []int{}, func(n int, acc []int) []int { + return append([]int{n}, acc...) + }) + assert.Equal(t, []int{1, 2, 3}, result) +} + +func BenchmarkFoldRight(b *testing.B) { + nums := make([]int, 1000) + for i := range nums { + nums[i] = i + } + + b.ResetTimer() + for i := 0; i < b.N; i++ { + u.FoldRight(nums, 0, func(n, acc int) int { return n + acc }) + } +}