7 ms·
> For loops take more reading to understand. A short comment would help here.
by JustSomeNobody 7y ago
> For loops take more reading to understand.
A short comment would help here.
- dwaltrip 7y agoThey could also improve the language. Map, filter, and reduce are such fundamental concepts.
- JustSomeNobody 7y agoI think Go is pretty refreshing as it is. If it isn’t to one’s taste there are 3712 other languages to choose from.
- deleted 7y ago[deleted]
- Improvotter 7y agoNo, Go does not need this and I do not want it to get these useless "features". Go is simple and it gets things done with an amazing standard library. Use a different programming language if this does not suite you. Every programming language ends up with its own implementation of these "concepts" that it is hard to keep track. What is so difficult about for loops? It's simple for everyone and you don't need to learn new concepts because it works.
- bauerd 7y agoThing is that simple != easy But oh man it's the same discussion every time when Go comes up here
- StillBored 7y agoFundamental? IMHO, they are just syntactic sugar on a foreach loop. Which in many ways is bad because it provides yet another way to express the same concept, without a significant difference in the method of execution. What am I missing?
- dwaltrip 7y agoMap and filter allow for a very concise and clear indication of the intention of the code, so it is much quicker for the reader to parse what it is doing. Edit: Also, for loops are just a way of implementing a mapping, where you have data A that you want transformed into data A*. The map is the fundamental concept here, not the for loop.
- StillBored 7y agoWhich is more generic, the 'for' or the 'map'? I think its the 'for' because it has additional capabilities contained in the base concept, which is why I would call it more "fundamental". In fact in many languages much of filter() can be implemented in the for construct rather than its body.
- kinjba11 7y agoThere are two sorts of 'fundamental'. You are correct that the 'for' is just syntactic sugar for 'goto' which is more fundamental than map/filter in the sense that it translates into assembly/machine language, upon which all programs are built. Then you can say the silicon logic gates are even more fundamental, you can do even more things with them, etc. The map/filter is more fundamental in a mathematical sense. If you turn a list of A turned into a list of A*, the 'mapping' is the fundamental concept, the 'for' loop is just an implementation detail. Maybe the computer did it in parallel, or in random order, or I asked you to turn a pile of towels into a folded stack of towels. The same way if I say 'want to watch a movie' - the movie the fundamental concept, whether it's digital, film, etc. is irrelevant.
- UK-Al05 7y agoI mean a for loop is just a syntatic sugar of a if and a go-to, but go-to is easier to mess up. Same with map, they are a constrained for each that is guaranteed to be a correct implementation.
- WoodTree 7y agoThey don’t have to be glorified for loops, even if they usually are. Map and Filter should be capable of running in parallel or asynchronously, and some languages provide this.
- edsonmedina 7y agoMaking them work with strong types would require generics which would introduce a lot of complexity to the language though. It's being debated for years. It's pretty trivial to write your own custom map/filter/reduce though.
- rco8786 7y agoDo you mean a code comment could help the reader understand, or you want a comment about why for loops are less obvious than other more functional approaches?
- JustSomeNobody 7y agoCode comment
- erik_seaberg 7y agoA for loop could mean anything, and not necessarily what the author intended. https://en.wikipedia.org/wiki/Rule_of_least_power https://en.wikipedia.org/wiki/Rule_of_least_power leads to list.filter(p) always calls p O(n) and returns a list of the same type with some or all of the same elements. list.map(f) always calls f O(n) times and returns a list of the same length. p and f can be reused and only depend on a single element. A good platform will inline list.filter(p).head into machine code that exits a loop early, that you don't have to write or review.