Implementing Map, Filter, And Reduce Using Generic In Go
Nov 15, 2022 · 2 min readThis is a short post showing an implementation of map, filter, and reduce using generic in Go.
Map
Map creates a new slice and populates it with the results of calling the provided function on every element in input slice.
Usage example:
Filter
Filter returns a new slice that contains elements from the input slice which return true
when they’re passed as a parameter to the provided testing function.
Usage example:
Reduce
Reduce runs a reducer function (the provided function) over all elements in the array, in ascending-index order, and accumulates them into a single value.
Every time, the return value of the reducer function is passed again to the next invocation of the function as the accumulator. Reduce returns the final value of the accumulator.
Usage example:
Wrap Up
That’s how we can implement map, filter, and reduce in Go. We can use these function just like how we use them in Python, JavaScript, Dart, Java, etc.