Hacker News new | ask | show | jobs
by skizm 3501 days ago
Is the first one two loops or one?
4 comments

Two. These methods are not lazy by default. Of course, you have to know the language pretty well to know that, but I do. For anyone wondering, you can write names.lazy.map(...) to get lazy evaluation.
The real answer is that it doesn't matter. Just like for most code, the output assembly or machine code doesn't matter.
It depends. Using lazy data structures, transducers, or a number of other common tricks would turn it into one loop. Haskell, Clojure, Ruby, Scala, Swift, and many other languages make it pretty easy to do.
Naïvely, two. The compiler can probably figure out that the semantics are the same and choose depending on what its model says will be faster.