14 points · by freetonik · 2y ago · 8 comments
It was really just feedback for the video. I felt the map and filter was super clear and great from the video, but I don’t quite understand how reduce is deciding what to remove from the two bowls at the beginning.
Map, filter, and reduce are not necessarily javascript functions. They are general concepts which can be found (or implemented) in various languages.
The point of reduce is to run the given function with the given data, repeatedly "reducing" the data. In the video, the function is "mix", which takes two arguments and mixes them together. The data is 3 bowls of fruit.
Reduce takes the function, then takes first two arguments, and passes them to the function. The function returns one piece of data (the mixed stuff), and now reduce passes that one piece together with the remaining third argument to another call of "mix". So in the end we have essentially mixed 3 elements together, even though "mix" accepts only 2 arguments. This is the reduction — a repeated application of some function on arbitrary amount of data.
So:
reduce([A, B, C], mix_fn)
is essentially converted into: mix(mix(A, B), C)
Hope that helps. Btw, in Python, there is functools.reduce() for this.