These are chat archives for ramda/ramda

27th
Oct 2018
guildwars-redhat
@guildwars-redhat
Oct 27 2018 18:04
(Didn't find any rules for the room, so sorry if this question is in the wrong place) A newbie/generic question for functional programming: Why is loop "not allowed"? (i.e., why would one care if one uses a mutable counter in a loop where the loop can't be seen outside the function)
Brad Compton (he/him)
@Bradcomp
Oct 27 2018 19:05

That's a great question @guildwars-redhat

First off, I should say that for the purposes of writing a library, we should be using the most efficient methods available to us. In JS that often means at the bottom we need loops. Recursion is just not that efficient in JS to avoid it. But that doesn't mean loops are a good pattern for FP, even if at some level, depending on the language, they are unavoidable.

I think my main issues with loops are that they are fundamentally procedural, stateful, and serial. Procedural because they rely on executing a series of steps in a block. Stateful because they involve declaring a mutable variable that changes over time. Serial because they can't be safely parallelized without examining the internals of what the loop is doing. Contrast that with something like map, which is declarative, stateless, and parallel. You could call the function on every item in the array at once without effecting the final value, it doesn't rely on any external state, and you don't need to keep track of the details of what is happening "under the hood".

They are also the wrong level of abstraction for application code. If you're using a raw loop, I can guarantee there is a clearer solution using a higher level pattern, be it map, reduce, or some more specific function. By using a loop instead you're basically rolling your own solution instead of taking advantage of these existing and well tested patterns. This adds more code that needs to be written, maintained, tested, and understood by those reading your function.

Lastly, I think , at least when you're learning, falling back to loops can end up preventing you from figuring out more idiomatic ways of doing things. You'll find the more you get used to functions like map, filter, any, etc., the less you'll feel yourself reaching for loops when you need to handle multiple pieces of data.

guildwars-redhat
@guildwars-redhat
Oct 27 2018 20:05
@Bradcomp Thanks very much, both for your time addressing a beginner question and for putting the answer in the context of FP principles and good practices. I'll surely be referring back to this answer as reinforcement of key concepts. If I could jump the gun, I had one follow up question, although it will most likely resolve itself as I make progress on the subject (so it doesn't need a response; it's only a question in passing): Is the statefulness mentioned above referring exactly to the mutability of the counter variable in a loop? (i.e., that the counter is exactly the unwanted state introduced by looping, and that we're not speaking about something else, such as the operations or effects of such operations carried out during each iteration of the loop, as the state)
Brad Compton (he/him)
@Bradcomp
Oct 27 2018 21:27
It’s inherent in that the loop is a statement and not an expression. So you have at the very least the mutable counter, as well as the state involved in whatever side effects are the purpose of the loop.