Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: When would you choose foreach instead of map?

by bl0rf (Pilgrim)
on May 20, 2004 at 22:33 UTC ( #355106=note: print w/ replies, xml ) Need Help??


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

map is fairly hideous and foreach looks much nicer. foreach also adds to the linguistic feel of Perl ( the smooth flow ) because it is intuitively understandable and shows off the benefits of arrays. Of course map has its own uses but I think that for simple cases foreach clutters the visual space much less...


Comment on Re: When would you choose foreach instead of map?
Select or Download Code
Re: Re: When would you choose foreach instead of map?
by rjray (Chaplain) on May 21, 2004 at 00:05 UTC

    Sorry, but I just cannot agree, or even let this lay unchallenged. map and foreach are indeed different operations, as others have pointed out to great clarity. And the reason they are both in the language is because of their differences, not their similarities.

    map is largely key to the "Schwartzian Transform". It not only produces a list as a side-effect (which foreach doesn't), it can produce very elaborate transformations of the input in the process. map is not hideous simply because someone doesn't understand it as well as foreach. Indeed, for those of us with a Lisp background, it's immediatly intuitive.

    --rjray

      It not only produces a list as a side-effect (which foreach doesn't), ...

      What on earth are you smoking?!? The entire point of map is to produce a list. The list production isn't a side-effect ... it's the whole effect. Modifying the original list is a side-effect.

      ------
      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

        Wow, and here I thought the purpose of map was to evaluation the block or expression over the range of the list passed to it. Perhaps you thought that by "side-effect", I meant that the creation of the output list was an accidental, unintentional thing. No. I meant "side-effect" in the same sense that most people use it with regards to programming.

        I'm quite well aware of the differences between map and foreach, and the conditions under which you would choose one or the other. Try to take a less confrontational tone, it will usually get you better responsiveness from others.

        --rjray

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://355106]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2014-09-16 18:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (41 votes), past polls