Re:Elixir for Cynical Curmudgeons
by bcrosby95 on Thursday August 03, 2023 @11:54AM
(#36987584)
Built ins aside, I find most uses of recursion to be better served by Enum.reduce.
[ Parent
| on HN ]
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.
Re:Elixir for Cynical Curmudgeons
by sodapopcan on Thursday August 03, 2023 @12:41PM
(#36988232)
I'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.
I assumed you meant something like Enum.sum that completely trivializes the code. In comparison, generic, higher level functions are pretty fundamental to FP.
Re:Elixir for Cynical Curmudgeons
by throwawaymaths on Thursday August 03, 2023 @02:22PM
(#36989665)
the 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