Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Unhappy returns

by Errto (Vicar)
on Oct 10, 2005 at 04:01 UTC ( [id://498699]=note: print w/replies, xml ) Need Help??


in reply to Unhappy returns

When I first learned about map in Perl, I assumed it behaved the same way as map in other languages I was using at the time - Haskell and ML. In these languages, the mapped function takes one argument and returns one value. This is equivalent to Perl's map using scalar context, which it perfectly well could have done. This was (presumably) an intentional design decision on Larry's part. Had it been the other way, we could guarantee that map would always return a list of exactly the same length as its argument list, and issues like the one you describe wouldn't arise. On the other hand, we wouldn't have the flexibility to return longer or shorter lists if appropriate. So it's a tradeoff.

Replies are listed 'Best First'.
Re^2: Unhappy returns
by Perl Mouse (Chaplain) on Oct 10, 2005 at 09:56 UTC
    It's a tradeoff, and I'm glad Larry made the decision map can return lists of arbitrary length. My prime use of this feature is to create hashes:
    my %hash = map {($_, 1)} qw /foo bar baz/;
    Perl --((8:>*

      Yes – though that still only returns a fixed number of elements per evaluation. What about flattening data structures?

      my @list = map @$_, @$lol;

      I think that feature of map is one of the most useful things to have around for those occasions it’s called for.

      Makeshifts last the longest.

      Using map is a lot of overhead for just assigning to a hash.
      use Benchmark; timethese(1000, { 'map1' => sub { my %str = map {($_,1)} (0..1024); }, 'arry' => sub { my %str = (); @str{0..1024} = (1)x1025; } }); Benchmark: timing 1000 iterations of arry, map1... arry: 1 wallclock secs ( 1.36 usr + 0.00 sys = 1.36 CPU) @ 73 +5.29/s (n=1000) map1: 3 wallclock secs ( 3.11 usr + 0.00 sys = 3.11 CPU) @ 32 +1.54/s (n=1000)
        Overhead how? Depending on what I'm doing, hash-slicing might be harder to maintain than map.
        my %is_month_abbrev = map { $_ => undef } qw( jan feb mar apr may jun jul aug sep oct nov dec ); # versus ... my %is_month_abbrev; @is_month_abbrev{ qw( jan feb mar apr may jun jul aug sep oct nov dec +) } = (undef) x 12; # or, with an intermediate array ... my @months = qw( jan feb mar apr may jun jul aug sep oct nov dec ); my %is_month_abbrev; @is_month_abbrev{@months} = (undef) x @months;
        Often, losing a few micro-seconds is worth readability and maintainability. In both the slicing solutions, I either needed a separate array or hard-coding a magic number. (The number of months in a year isn't always a given. Some calendars have as few as 10, others have up to 14, and still others vary from year to year.)

        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?
        That of course depends entirely on what you do with in the map. I used constant values of 1 just to illustrate my point, and to keep the example as simple as possible.
        Perl --((8:>*

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2025-06-22 18:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.