Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: map versus for

by dragonchild (Archbishop)
on Aug 04, 2008 at 15:06 UTC ( [id://702079]=note: print w/replies, xml ) Need Help??


in reply to map versus for

map can be faster because it's theoretically parallelizable, unlike for which is (generally) not.

The bigger point, though, is that by using map, you're telling me more about your intent with the code. map says "I'm doing something to each element, something that's probably easily described, and accumulating the result." On the other hand, for says "I'm doing something with each element and it could be anything."


My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

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

    That's pretty much the distinction I'd make as well, although I usually phrase it another way: for is for generic iteration, map is specifically a transformation.

    A surefire way to annoy me and lose points when we get sample code is people who for whatever reason use map in void context rather than a proper for loop (it doesn't say "LOOK I R IDIOMATIC CODERZ", it says "ITERATION: UR DOIN IT WRONG" (and yes, I do have a lolcat based scale for applicants :)).

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

      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

        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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-23 11:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found