6 ms·
A for-each loop is only slightly longer, more familiar (just about every language has it), and more flexible - it covers map, flatMap, and reduce. So why have t
by skybrian 10y ago
A for-each loop is only slightly longer, more familiar (just about every language has it), and more flexible - it covers map, flatMap, and reduce. So why have three or more specialized constructs when one will do?
There are cases when the restrictions of map() and reduce() are necessary, most famously for a map-reduce in distributed programming, but that's not really relevant for implementing a simple function.
- macintux 10y agoI would argue that for loops are imperative, map/fold/etc are more declarative. That leads to various benefits such as less code, fewer bugs, fewer off-by-one errors. And, man, tail recursion in a language with function head pattern matching (ML family, Erlang) is so much easier to read than any complicated for loop. (Update: realized afterwards that "foreach" isn't quite the same as "for", but the larger point still stands, mostly.)
- jghn 10y agoWhen I was first introduced to scala my very favorite thing was the existence of map. I'd been using java for a while and missed it dearly
- pjmlp 10y agoNot allowed to use Java 8?
- jghn 10y agoDidn't exist at the time. While I still prefer scala to java 8, had the latter been available at the time I likely never would have dabbled in scala in the first place, if that makes sense
- pjmlp 10y agoSure it makes sense. I dabble in what sorts of programming languages, but when comes to work, it is mostly C#, Java, JavaScript and some occasional C++, because that is what customers pay for. Hence why I see a big value in mainstream languages slowly evolving into multi-paradigm ones, as many of us don't have the luxury to move beyond the first class languages of each platform.
- danellis 10y agoThe problem with for-each in a lot of languages is that it's a statement, not an expression, so it only works with mutable data.
- hyperpape 10y agoI'm definitely in camp map/filter/fold, but if you instantiate a list/accumulator, fill/operate on it inside the loop, then consume it, you're essentially writing pure code. The fact that you're doing it on top of mutable foundations doesn't matter (at that scale).
- fpgeek 10y agoAs long as you get your mutable code right. If there are any unintentional holes in your instantiate/operate/consume box...
- hyperpape 10y agoThere just isn't that much to get wrong in pure for-each loops: def filter_broken(widgets): broken_widgets = [] for widget in widgets: if widget.is_broken(): broken_widgets.append(widget) return broken_widgets The actual errors with loops and mutable objects come from complex nested loops, continues/breaks (multi-level ones especially!), extra boolean conditions, and sharing mutable objects between parts of the code base. I think that overstating the supposed risks of for-loops doesn't help anything. The problem is not that you're going to mess up a simple for loop. It's the ways that your code doesn't compose well that are more problematic. Oh, and the verbosity sucks too.
- macintux 10y agoHm. Wouldn't your code reverse the order of the list? Maybe that's right, maybe that's wrong, but it's indeterminate from the code whether it's desired, whereas a filter function and a reverse function would make it explicit.
- agumonkey 10y agoside effects ? testing ? decoupling ? (thus reuse) If your brain is organized enough to write clean for loops then ... maybe. But it's a bit opportunity for problems. At least to me; but then I may not be smart enough.
- Daishiman 10y agoMap, fold, reduce and friends are statements of intent, and reading a line of transformations composed on multiple maps and such is generally much clearer than reading a series of loops in which you have to figure out intent by looking and what's going on inside each loop. Also, error density is relatively constant per line of code according to most studies (this is a very general statement, but we can assume that most code just isn't trying to be clever). I've forgotten to add(), append() and drop items inside a loop far more than I've made mistakes inside more functional methods.
- deepGem 10y agoI think for tasks such as JSON parsing, map and flatmap like functions are really helpful. You write less code and the code is way easier to understand (for those who are familiar with map functions). That being said, map and flatmap force immutability in some way. So you pay a price for this, either in speed or in memory, even with tail recursion.
- pjmlp 10y agoNo it doesn't, because map, flatMap, and reduce can be lazy, distributed across local processors, or even the network. They describe the intent of what to do with the data, not how it is done. Just like using SQL versus an old xBase database to access data records.
- lmm 10y ago> So why have three or more specialized constructs when one will do? Because having three specialized functions for three specialized operations makes it easier to see what a given piece of code is doing. One function per function.
- ptx 10y agoAnd a goto statement is even more general, covering all kinds of looping constructs, exceptions and even the eliminating the need for functions! Why not just use those?
- unscaled 10y agoFor loops are yet another of these useless abstractions introduced by the academia, this time straight from the design-by-committee language ALGOL 60. We all know real programmers used gotos.
- lostcolony 10y agoTechnically, reduce covers map, flatmap, filter, etc. It's every bit as flexible as a for-each loop (the only difference is it implies no side effects, but does not guarantee them if the language doesn't). The very reason those other functional constructs were split out was not due to need, but due to -clarity-. And that same clarity applies to why to use one of those constructs instead of a for-each loop.