Skip to content

Commit 0aa2b68

Browse files
authored
Remove reference to term "accumulator" (#773)
1 parent 9e0e9a2 commit 0aa2b68

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

arrays/v8/collection_fun.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ func Find[A any](items []A, predicate func(A) bool) (value A, found bool) {
99
return
1010
}
1111

12-
func Reduce[A, B any](collection []A, accumulator func(B, A) B, initialValue B) B {
12+
func Reduce[A, B any](collection []A, f func(B, A) B, initialValue B) B {
1313
var result = initialValue
1414
for _, x := range collection {
15-
result = accumulator(result, x)
15+
result = f(result, x)
1616
}
1717
return result
1818
}

revisiting-arrays-and-slices-with-generics.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ You should be familiar with the generics syntax [from the previous chapter](gene
6666

6767
If you think about the arguments to your function first, it'll give you a very small set of valid solutions
6868
- The array you want to reduce
69-
- Some kind of combining function, or _accumulator_
69+
- Some kind of combining function
7070

7171
"Reduce" is an incredibly well documented pattern, there's no need to re-invent the wheel. [Read the wiki, in particular the lists section](https://en.wikipedia.org/wiki/Fold_(higher-order_function)), it should prompt you for another argument you'll need.
7272

@@ -75,10 +75,10 @@ If you think about the arguments to your function first, it'll give you a very s
7575
### My first-pass of `Reduce`
7676

7777
```go
78-
func Reduce[A any](collection []A, accumulator func(A, A) A, initialValue A) A {
78+
func Reduce[A any](collection []A, f func(A, A) A, initialValue A) A {
7979
var result = initialValue
8080
for _, x := range collection {
81-
result = accumulator(result, x)
81+
result = f(result, x)
8282
}
8383
return result
8484
}
@@ -265,10 +265,10 @@ But this won't compile.
265265
The reason is we're trying to reduce to a _different_ type than the type of the collection. This sounds scary, but actually just requires us to adjust the type signature of `Reduce` to make it work. We won't have to change the function body, and we won't have to change any of our existing callers.
266266

267267
```go
268-
func Reduce[A, B any](collection []A, accumulator func(B, A) B, initialValue B) B {
268+
func Reduce[A, B any](collection []A, f func(B, A) B, initialValue B) B {
269269
var result = initialValue
270270
for _, x := range collection {
271-
result = accumulator(result, x)
271+
result = f(result, x)
272272
}
273273
return result
274274
}

0 commit comments

Comments
 (0)