The Reduce function
Next, let's look at the Reduce function. Reduce is the Swiss army knife of HOFs. With a Reduce function, we can do anything that can be done with Filter or Map.
A Reduce function, also known as a fold, accumulate, aggregate, compress, or inject takes a seed value and applies the logic of the reducer function to the seed, and potentially multiple calls to itself to arrive at a result. Often, the reduce function will combine the data elements to return a single aggregated value, hence the term fold. So, we fold all of the data into a single result.
Back in main.go, we apply the ByMake filter to filter out all the cars that are not a Honda product. Then, we call the Reduce function to transform the collection of Honda vehicles ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access