4 ms·
Yeah I knew someone was going to say that (see last sentence), but a for loop is more powerful and general than reduce(). Mutability within the loop is useful,
by chubot 3y ago
Yeah I knew someone was going to say that (see last sentence), but a for loop is more powerful and general than reduce().
Mutability within the loop is useful, and it can be controlled by processes and message passing.
- bcrosby95 3y agoI assumed you meant something like Enum.sum that completely trivializes the code. In comparison, generic, higher level functions are pretty fundamental to FP.
- sodapopcan 3y agoI'm not sure "powerful" is the right word here, it's more a matter of "what certain people are used to". If you want a counter or whatever, you can use a tuple to give yourself multiple accumulators. You could argue it's uglier, but I wouldn't necessarily agree. It's something that is easy to get used to if you remove the "but I'd rather just do it this way" mindset.
- throwawaymaths 3y agothe problem with a for loop is that it is inexplicit about what from the outer scope can be mutated. Which isn't a problem, for small functions but I have definitely seen monster for loops in professionally written django code with ~500 lines of code in the function. Besides being in that magical sweet spot (executes imperatively but reads declaratively), a reduce forces you to declare ahead of time what things can be carried over and mutated between each iteration. > it can be controlled by processes and message passing. DO NOT DO THIS YOU WILL REGRET THIS SIMCITY MEME