Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove reference to term "accumulator" #773

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions arrays/v8/collection_fun.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ func Find[A any](items []A, predicate func(A) bool) (value A, found bool) {
return
}

func Reduce[A, B any](collection []A, accumulator func(B, A) B, initialValue B) B {
func Reduce[A, B any](collection []A, f func(B, A) B, initialValue B) B {
var result = initialValue
for _, x := range collection {
result = accumulator(result, x)
result = f(result, x)
}
return result
}
10 changes: 5 additions & 5 deletions revisiting-arrays-and-slices-with-generics.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ You should be familiar with the generics syntax [from the previous chapter](gene

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

"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.

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

```go
func Reduce[A any](collection []A, accumulator func(A, A) A, initialValue A) A {
func Reduce[A any](collection []A, f func(A, A) A, initialValue A) A {
var result = initialValue
for _, x := range collection {
result = accumulator(result, x)
result = f(result, x)
}
return result
}
Expand Down Expand Up @@ -265,10 +265,10 @@ But this won't compile.
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.

```go
func Reduce[A, B any](collection []A, accumulator func(B, A) B, initialValue B) B {
func Reduce[A, B any](collection []A, f func(B, A) B, initialValue B) B {
var result = initialValue
for _, x := range collection {
result = accumulator(result, x)
result = f(result, x)
}
return result
}
Expand Down
Loading