3 ms·
I find the first one to be far easier to read. You can basically just read it from top to bottom and know exactly what it does. "Take names, turn each one into
by mikeash 10y ago
I find the first one to be far easier to read. You can basically just read it from top to bottom and know exactly what it does. "Take names, turn each one into a Person, keep the ones that are valid."
The second one takes a lot more work. First, I have to read through the code and recognize that this is a loop that accumulates values into a new array. Then I have to pick it apart and see exactly where the accumulation takes place, and how that value is derived. It's not hard by any means, but the first one is far more straightforward.
- skizm 10y agoIs 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.