3 ms·
How is .each any simpler than a for loop, conceptually they do the same thing.
by ioquatix 14y ago
How is .each any simpler than a for loop, conceptually they do the same thing.
- pbiggar 14y agoRead the article.
- ioquatix 14y agoYeah, I did...
- pbiggar 14y agoNot well. It's described for the entire final quarter of the article, in the section marked "Why .each Is A Horrible First Looping Construct".
- evincarofautumn 14y ago“for” is the raw loopy stuff out of which other looping constructs are made—it’s fairly arbitrary. If you’re implementing an iteration construct, then by all means “for” is the thing to use. But in normal use it’s best to express your intent through more common, composable constructs such as maps, folds, and zips. There’s certainly less overhead in reading a non-trivial loop written as a composition than one written as a raw “for”. So I don’t think “each” is any better than “for”—in addition to having uglier syntax in Ruby, neither conveys any more information than the other. It’s the same with std::for_each in C++, present only for consistency with its more meaningful siblings.
- pbiggar 14y agoI have no dog in this fight, I was just calling out the hyperbole on the ancestor comment.
- ioquatix 14y agoIf you didn't get my intent, the last part of the article which you recommended was highly biased and after reading it I was left questioning the author's motive.
- pbiggar 14y agoI don't see the bias or any reason to question his motive, so no, your intent was not clear. I think you'll need to spell this one out for me.