in reply to When would you choose foreach instead of map?

map is a list-level operator. foreach is a scalar-level operator. That they can often be rewritten in terms of each other is neat, but not much more.

I use map when I want to transform one list into another list. So, in your example, I would use map to transform the list.

I use foreach when I want to do complex logic with each item separately. Now, I can use map for nearly every instance I use foreach, but it would require me to think more. As a Perl developer, I'm constitutionally lazy, so I'd rather not think more than I have to. So, I don't. :-)

------
We are the carpenters and bricklayers of the Information Age.

Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

I shouldn't have to say this, but any code, unless otherwise stated, is untested

  • Comment on Re: When would you choose foreach instead of map?