3 ms·
This is interesting to me because the map reduce has way less cognitive load for me. With map reduce I see the following: - We've got an array - We're going
by nulagrithom 9y ago
This is interesting to me because the map reduce has way less cognitive load for me.
With map reduce I see the following:
- We've got an array
- We're going to loop through it and square everything
- We're going to take that and add it all together
- The result is r
When I read the for-loop counterpart:
- We have a variable called r that we're initializing to 0
- We're starting a loop with j as the index initialized to 0 iterating through an array
- We've got a variable x on each iteration that is the current position in the array
- We're adding the square of x to r
Maybe it's a personal quirk. for-loops make me feel stupid.
- dvt 9y agoThe biggest issue I have is with this step: > - We're going to take that and add it all together Here, we have an imaginary variable: namely, "that." Something that we need to keep track of and is not explicitly referenced anywhere. It's relatively easy when you only have one of these things, but with complex map-reducer logic, you can end up having a whole bunch of these. In the for-loop, everything I use is explicitly referenced. There's no "that" -- everything is "r" or "x" or "j."
- amzans 9y agoThe main advantage that I’ve seen when writing and reading functional instead of procedural/imperative code, is that the intention is usually clear. We have a list of numbers that we want to square and add up, instead of the more mechanical steps with the loop approach.