Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^5: map versus for

by actualize (Monk)
on Aug 04, 2008 at 21:09 UTC ( [id://702179]=note: print w/replies, xml ) Need Help??


in reply to Re^4: map versus for
in thread map versus for

I think I get it: Why use a gun to kill a cockroach when you have a perfectly good shoe. Since you don't get the benefits of mapping the data, and as such you are wasting time and space, it's better that you use "for".

So, if I am understanding you correctly,

  • for: used for iterating an action over a series of items.
  • map: used when one needs to create create a table of data which will be used at a future time.
  • -Actualize

    Replies are listed 'Best First'.
    Re^6: map versus for
    by blazar (Canon) on Aug 05, 2008 at 22:07 UTC
      map: used when one needs to create create a table of data which will be used at a future time.

      I personally believe that you nearly got it: more precisely, map is used whenever you clearly have an input list that you want to be (functionally) transformed in an output list. In some sense your reference to the "future time" is correct even if this future may be just as close to the map() itself as in "being in the very same statement." At the same time, you can also set side effects within the map() block (both to some "external" variables and to the elements of the input list themselves) but you generally don't want to do that. It just... doesnt' smell like that:

      C:\temp>perl -E "@a=1..3; @b=map 2*($_*=2) => @a; say qq|A = [@a], B = + [@b]|" A = [2 4 6], B = [4 8 12]
      --
      If you can't understand the incipit, then please check the IPB Campaign.

    Log In?
    Username:
    Password:

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

    How do I use this?Last hourOther CB clients
    Other Users?
    Others taking refuge in the Monastery: (6)
    As of 2024-04-19 08:59 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found