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

Re^3: map versus for

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


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

I have done some research on best practices regarding map. I wanted to ask at one point why we don't just use map instead of foreach. However I found a few nodes about that subject where, as you just did, map in a null context was brought up. However, I am not sure I understand what that means. Could you describe what map in a null context is?

-Actualize

Replies are listed 'Best First'.
Re^4: map versus for
by Fletch (Bishop) on Aug 04, 2008 at 20:50 UTC

    Using map but not capturing the returned values:

    map { something( $_ ) } @somelist;

    In recent versions that's been optimized (basically the return values are silently discarded rather than a temporary list built and then discarded later) so it's not as blecherous performance wise.

    However it really buys you nothing to use it instead of a for loop here, because you've now muddled the conceptual waters (Was it at one point using the returned values and changed? Did they plan on possibly using them at some point?) and makes the code harder to understand (rather than the important thing (what's being iterated over) being up front, you've got to read past the details (what's being done for each item) to find out). It's along the lines of using passive or active voice in a sentence ("The cow jumped over the moon." vs "The moon was jumped over by the cow"); using the wrong one can shift what the reader takes as the emphasis to the wrong part.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      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
        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://702166]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-03-28 10:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found