5 ms·
Is the first one two loops or one?
by skizm 10y ago
Is the first one two loops or one?
- actsasbuffoon 10y agoIt 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.
- mikeash 10y agoTwo. 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.
- chii 10y agoThe real answer is that it doesn't matter. Just like for most code, the output assembly or machine code doesn't matter.
- andreareina 10y agoNaï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.