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
Latest comments (40)
I use higher order functions (map, filter, fold) whenever reasonable - easier to think about and maintain in the long run.
Really like the forEach in Java for anything you don't need the return value for. With the logic in a separate method it makes the code both short and readable.
When I do need the return value is stream, and ending in some collect.
But there are still some cases where you need to integration number in the loop, so back to for loops in that case.
With Clojure it's for if I want the return value, or doseq if I don't. It's also common to use reduce for looping when you need the return value.
After reading previous reactions maybe I should give Go a go. Having only one way to do things seems boring. But now I'm sometimes busy with Java changing the loops to someone else's taste to get the merge request approved.
I generally work with collections in C# so I use ForEach loops a lot.
In PHP the implementation of collections is quite popular now. This makes map and reduce a breeze. It also makes it more readable.
However I am curious to know why not many people know about and have implemented a Duff’s Device.
This is probably the most optimized way of iteration over items. My take is because it is less readable or understandable.
I have implemented this approach both in JavaScript and PHP. And it is quite faster than normal iterations. I think it can be implemented in any language that supports a while loop.
However only use it where optimizations are needed.
I'm a fan of ruby's each method for the fact that you can use it as an interface to make any object enumerable (or what other languages call iterable).
I particularly love it for the lazy evaluation. You can iterate over a collection of infinite size like a prime number generator or like something I did the other day, wrap a large remote resource but make it feel like any other local one. You just stream in as much of it as you need and no more.
JavaScript
In production I prefer map, reduce, etc. because I always want to return the same type as input. Sometimes I do use for loop when writing temporary utility scripts, or trying to validate some ideas. It's just because for loop is easy to read even without much context.
Loop is boring, and recursion is my choice.
hehe)
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
I just published this
Let's loop - for...in vs for...of
Laurie ・ Jul 16 ・ 4 min read
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).