You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: revisiting-arrays-and-slices-with-generics.md
+5-5
Original file line number
Diff line number
Diff line change
@@ -66,7 +66,7 @@ You should be familiar with the generics syntax [from the previous chapter](gene
66
66
67
67
If you think about the arguments to your function first, it'll give you a very small set of valid solutions
68
68
- The array you want to reduce
69
-
- Some kind of combining function, or _accumulator_
69
+
- Some kind of combining function
70
70
71
71
"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.
72
72
@@ -75,10 +75,10 @@ If you think about the arguments to your function first, it'll give you a very s
75
75
### My first-pass of `Reduce`
76
76
77
77
```go
78
-
funcReduce[A any](collection []A, accumulatorfunc(A, A) A, initialValue A) A {
78
+
funcReduce[A any](collection []A, ffunc(A, A) A, initialValue A) A {
79
79
varresult = initialValue
80
80
for_, x:=range collection {
81
-
result = accumulator(result, x)
81
+
result = f(result, x)
82
82
}
83
83
return result
84
84
}
@@ -265,10 +265,10 @@ But this won't compile.
265
265
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.
266
266
267
267
```go
268
-
funcReduce[A, B any](collection []A, accumulatorfunc(B, A) B, initialValue B) B {
268
+
funcReduce[A, B any](collection []A, ffunc(B, A) B, initialValue B) B {
0 commit comments