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

Re: Things I Don't Use in Perl

by derby (Abbot)
on Aug 23, 2005 at 16:25 UTC ( #485950=note: print w/ replies, xml ) Need Help??


in reply to Things I Don't Use in Perl

What the heck, I could use some downvotes - I'm not a fan of map. Hey ... even the doc says it's just a funny way to write a foreach loop.

-derby


Comment on Re: Things I Don't Use in Perl
Re^2: Things I Don't Use in Perl
by danmcb (Monk) on Aug 23, 2005 at 18:46 UTC

    Almost. Map returns an array, which a foreach doesn't AFAIK. So you can get some useful behaviour with:

    join "\n", map{$_->someFunction()} @a;

      Map returns a list
Re^2: Things I Don't Use in Perl
by fizbin (Chaplain) on Aug 29, 2005 at 00:41 UTC
    Maybe it's just that I was a math major, or got too close to those whacky Haskell nuts, but map has always seemed a very natural metaphor for me. (It's the natural extension of a function on a space A to the space An)

    However, this (map is natural instead of foreach) is only true when the expression inside the map isn't itself accumulating side-effects; e.g. the following useage of map doesn't seem natural:

    my $i=0; map {$keywordhash{$_}=(++$i);} @keywords;
    However, this useage seems quite natural:
    %keywordhash = map {$_ => 1} @keywords;
    I guess you could also distinguish these two cases by noting that one calls map in void context whereas the other does not; while that's true, I think I'd still prefer
    do {$keywordhash{$_}=(++$i);} for @keywords;
    over:
    %keywordhash = map { $_ => ++$i } @keywords;
    Even though I like map, because I'm not completely comfortable with the idea of depending on a certain execution order for map, even though I know it has one. When I'm thinking in a linear, this-gets-executed-then-this mode, map rarely makes sense. map does however make sense as a higher-order function, and when I'm thinking like that it's quite natural.

    (Of course, all style niceties get completely discarded inside JAPHs)

    --
    @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2015-07-06 01:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (68 votes), past polls