4 ms·
As programmers, we often confuse the means with the ends. We tend to over-emphasise the way in which code is written and we tend to forget about whether the cod
by kaeluka 10y ago
As programmers, we often confuse the means with the ends. We tend to over-emphasise the way in which code is written and we tend to forget about whether the code is doing the right thing.
That being said, I love map/filter/flatMap. Look at them as higher level loops.
For example: A while loop is very general, you can implement any kind of loop with it. This makes it harder to understand the meaning of the loop. Therefore, when you want to iterate a constant number of times, you'll likely use a for loop. It expresses your intent better. Map, filter, and flatMap are just extending this principle to more specialised use cases. The advantage for a reader is that the one word "map" already tells them a lot about what the loop body is good for.
- heisenbit 10y agoMaybe it is a higher level of looping and certainly looks pleasing. However when it comes to using a debugger I have not found a way to avoid a higher level of effort.
- zigzigzag 10y agoIntelliJ's debugger has decent support for working with functional code. Also the IDE can convert between map/filter/fold type functional pipelines and imperative loops automatically.
- general_ai 10y ago_Any_ debugger has _excellent_ support for working with imperative code. And not everyone uses an IDE, nor should it be a requirement.
- kaeluka 10y agoWhat you say is true on the JVM. However, it is important to note that this does not need to be that way. One could imagine having the higher level loops and still debug them. Of course, if you're writing actual code, now -- what I'm saying doesn't help you much. But if you're looking at a new language, it might be important to remember: not "filter" is broken that way, it's just the implementation on the JVM.
- kochthesecond 10y agoThis is the way I see it. A for loop is good if I'm counting, if I'm applying a function to every element in a list, I should express that. An added bonus is that null elements are sorted and that you HAVE to separate data manipulation operations. You have to filter, map and apply sorting separately, not mix all three in a triple for loop with several branches.