I was thinking about how in Ruby, we rarely use while
or for
even though they are available. The language prefers array.each do
loops or perhaps n.times do
.
How does your language handle these sorts of things?
Do you like the way it's done?
I was thinking about how in Ruby, we rarely use while
or for
even though they are available. The language prefers array.each do
loops or perhaps n.times do
.
How does your language handle these sorts of things?
Do you like the way it's done?
For further actions, you may consider blocking this person and/or reporting abuse
Chris Jarvis -
Oliver Bennet -
Alexander Shagov -
Jess Lee -
Top comments (40)
Go only has for loops so there's not much of a choice there 😂
In Python I use for as well combined with various iterators. List comprehensions are favored over map and reduce there.
I like go's for a lot. It can be a for, a while, or an infinite loop. It's all in how you use it.
The same idea shows up all over the place in it: "here's some minimal core, fiddle it however you want". It's one of its main attractions ♥️
Exacltly. I was ready to post the same about Go xD
Honestly it's one of many reasons Go is great, no bikeshedding about iteration
Though go is kind of cheating since it's "for" loop can behave in many ways.
The for loop in Go is more flexible than the one in C though. I just started with Go, and it's surprising how much they chose to diverge from other C-like languages.
I'm actually writing a post which touches on this exactly. Figured I might as well post the relevant part of it here:
A vanilla for loop is one of the least parallel constructs that exists in programming. At my last job, one of the teams I led spent months trying to come up with a strategy to convert traditional
R
lang for-loops into automagically parallel code. It's basically an impossible problem with modern technology. The difficulty arises because of a single pattern possible with for loops.This code only produces the intended result if it is executed in order, iteration by iteration. If you tried to execute multiple iterations at once, the processor might incorrectly branch based on inaccurate values, which invalidates the result. We would be having a different conversation if this was C code, as the usage is different and there are quite a few tricks the compiler can do with loops. In JavaScript, traditional for loops should only be used if absolutely necessary. Otherwise utilize the following constructs:
map
for-each
Do you know JS has async iterators and generators?
Yes, and I do everything in my power to stay the hell away. Why would I want an
async
for loop? That's just a map with extra steps.Probably the use case hasn't really emerged well. Maybe with an infinite stream of data or if you have to build a lazy parser for something like HTML or XML...
Lately, I am liking this way of writing fors in JavaScript:
But I have always used the 'indexed' version:
There are other ways (forEach for example) but as I know they are usually 'slower' so I always use for :)
I think that depends on how much data you're processing. If it's a massive amount, then you may notice it to be a bit slower, but generally the discrepancy is so minimal that it's inconsequential. v8 is actually greatly optimised to use
forEach
. This is a great talk on the subject by Mathias Bynens: youtube.com/watch?v=m9cTaYI95ZcSo, IMO, it's a far nicer developer experience to use
forEach
orfor of/in
in certain cases.F# has some traditional looping constructs like
for
andwhile
. However, they provide functions that take all the common plumbing out of looping. For example, say you want to loop through a list of items and convert each one to a string. You only have to define how to do it for one item.Then you use a List operation to perform that transformation on a whole list. Without having to worry about the mechanics of looping through it.
I really like the separation of concerns. My
stringify
function only worries about a single item... it doesn't know anything about lists. And sinceList
introduces the need to loop, it also provide functions to do that for you (map
,filter
,reduce
, etc). You simply plug in a function for a single item, and it takes care of doing it to the whole list.In Rust it's quite a simple syntax:
And for
Vec
:Same syntax for everything \o/
Or, more realistic example from a project I'm currently working on:
Common Lisp's loop can do crazy amount of things in a compact form, though there are things that are missing, and the whole thing isn't extensible (though there are library-provided alternatives like iterate that are better in this aspect).
Ruby is unique in the “more than one way to do the same thing” and while (no pun intended)
for/while
loops may not be as common, they were made for developers entering ruby from other languages particularly java and php (I don’t have source for this, will look it up)Ruby is not as unique in that respect as you think.
There are not one, not two, but three ways to loop over an array in JavaScript, and that's not counting special cases like map, reduce, or filter. You have classic C-style for loops, 'for...of' loops (iteration), and the callback-based
.forEach()
approach.I always used to use foreach in php, but now I’ve swapped to using collection methods in Laravel as they are much more powerful and you can chain them together instead of having multiple loops or nested loops.
Here is an example of map, taken from the docs, which I use a lot
Would prefer to use
forEach
more but for loops are faster in older javascript engines so thats what I've been using latelyIn JavaScript I only use loops in special cases.
Normally, I use filter/map/reduce.
When I have an array of promises I use a for-of-loop with await.
When I see perf problems I use plain for-loops.