Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Syntactic Confectionery Delight
 
PerlMonks  

Re: Efficiency of map vs. more verbose basic/fundamental code

by eyepopslikeamosquito (Canon)
on Oct 04, 2012 at 22:47 UTC ( #997338=note: print w/ replies, xml ) Need Help??


in reply to Efficiency of map vs. more verbose basic/fundamental code

The later edition of Effective Perl Programming has item 20, "Use foreach, map and grep as appropriate", which gives an excellent summary of when to use foreach, map and grep:

  • Use foreach to iterate read-only over each element of a list
  • Use map to create a list based on the contents of another list
  • Use foreach to modify elements of a list
  • Use grep to select elements in a list

Hall, McAdams and foy further caution against modifying a list via map:

"For efficiency, $_ is actually an alias for the current element in the iteration. If you modify $_ within the transform expression of a map, you modify the input data. This is generally considered to be bad style, and -- who knows? -- you may even wind up confusing yourself this way. If you want to modify the contents of a list, use foreach."

I'm pretty sure the earlier edition you are reading has a similar item. If you read this item, you should not be confused about which one to use. I suggest you focus on clarity and only bother benchmarking if performance is really critical.


Comment on Re: Efficiency of map vs. more verbose basic/fundamental code
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (12)
As of 2014-04-16 23:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (436 votes), past polls