Skip to content

Commit 599dc86

Browse files
committed
slices: change ReduceWith to ReduceFrom and add comments
1 parent 8a27a37 commit 599dc86

File tree

4 files changed

+22
-19
lines changed

4 files changed

+22
-19
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ require (
3333
github.com/upfluence/log v0.0.5
3434
github.com/upfluence/stats v0.1.4
3535
github.com/upfluence/thrift v2.4.4+incompatible
36+
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0
3637
golang.org/x/oauth2 v0.19.0
3738
golang.org/x/sync v0.8.0
3839
golang.org/x/term v0.13.0
@@ -57,7 +58,6 @@ require (
5758
go.uber.org/atomic v1.6.0 // indirect
5859
go.uber.org/multierr v1.5.0 // indirect
5960
golang.org/x/crypto v0.14.0 // indirect
60-
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
6161
golang.org/x/sys v0.13.0 // indirect
6262
google.golang.org/protobuf v1.31.0 // indirect
6363
gopkg.in/inf.v0 v0.9.1 // indirect

go.sum

+4-6
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
178178
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
179179
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
180180
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
181-
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
182-
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
181+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
182+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
183183
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
184184
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
185185
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
@@ -560,8 +560,7 @@ golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJ
560560
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
561561
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
562562
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
563-
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ=
564-
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
563+
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
565564
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
566565
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
567566
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
@@ -618,9 +617,8 @@ golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgw
618617
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
619618
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
620619
golang.org/x/tools v0.0.0-20200103221440-774c71fcf114/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
621-
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
622-
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
623620
golang.org/x/tools v0.25.0 h1:oFU9pkj/iJgs+0DT+VMHrx+oBKs/LJMV+Uvg78sl+fE=
621+
golang.org/x/tools v0.25.0/go.mod h1:/vtpO8WL1N9cQC3FN5zPqb//fRXskFHbLKk4OW1Q7rg=
624622
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
625623
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
626624
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

slices/reduce.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@ package slices
22

33
import "golang.org/x/exp/constraints"
44

5-
func ReduceWith[S ~[]E, E any, A any](s S, acc A, fn func(A, E) A) A {
6-
for _, e := range s {
7-
acc = fn(acc, e)
5+
// ReduceFrom will iterate over each element of slice and call reduceFn for each.
6+
// The return value of each reduceFn is passed to the next call of reduceFn as acc, or
7+
// in other term at each iteration on the slice.
8+
func ReduceFrom[S ~[]E, E any, A any](slice S, acc A, reduceFn func(acc A, elem E) A) A {
9+
for _, e := range slice {
10+
acc = reduceFn(acc, e)
811
}
912

1013
return acc
1114
}
1215

13-
func Reduce[S ~[]E, E any, A any](s S, fn func(A, E) A) A {
16+
// Reduce does the same as ReduceFrom but use the zero value of A as
17+
// the first value to be passed to reduceFn.
18+
func Reduce[S ~[]E, E any, A any](slice S, reduceFn func(acc A, elem E) A) A {
1419
var acc A
1520

16-
return ReduceWith(s, acc, fn)
21+
return ReduceFrom(slice, acc, reduceFn)
1722
}
1823

1924
type sumable interface {

slices/reduce_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestReduceWith(t *testing.T) {
1919
{slice: []int{1}, acc: 0, fn: func(a, b int) int { return a + b }, want: 1},
2020
} {
2121
t.Run("", func(t *testing.T) {
22-
got := ReduceWith(tt.slice, tt.acc, tt.fn)
22+
got := ReduceFrom(tt.slice, tt.acc, tt.fn)
2323
assert.Equal(t, tt.want, got)
2424
})
2525
}
@@ -43,15 +43,15 @@ func TestReduce(t *testing.T) {
4343

4444
func TestSum(t *testing.T) {
4545
t.Run("integer", func(t *testing.T) {
46-
assert.Equal(t, Reduce([]int{1, 2}, Sum), 3)
47-
assert.Equal(t, ReduceWith([]int{1, 2}, 22, Sum), 25)
46+
assert.Equal(t, 3, Reduce([]int{1, 2}, Sum))
47+
assert.Equal(t, 25, ReduceFrom([]int{1, 2}, 22, Sum))
4848
})
4949
t.Run("float", func(t *testing.T) {
50-
assert.Equal(t, Reduce([]float64{1.1, 2.3}, Sum), 3.4)
51-
assert.Equal(t, ReduceWith([]float64{1.1, 2.2}, 22.22, Sum), 25.52)
50+
assert.Equal(t, 3.4, Reduce([]float64{1.1, 2.3}, Sum))
51+
assert.Equal(t, 25.52, ReduceFrom([]float64{1.1, 2.2}, 22.22, Sum))
5252
})
5353
t.Run("string", func(t *testing.T) {
54-
assert.Equal(t, Reduce([]string{"foo", "bar"}, Sum), "foobar")
55-
assert.Equal(t, ReduceWith([]string{"foo", "bar"}, "buz", Sum), "buzfoobar")
54+
assert.Equal(t, "foobar", Reduce([]string{"foo", "bar"}, Sum))
55+
assert.Equal(t, "buzfoobar", ReduceFrom([]string{"foo", "bar"}, "buz", Sum))
5656
})
5757
}

0 commit comments

Comments
 (0)