<?xml version="1.0" encoding="windows-1252"?>
<node id="487310" title="Re^2: Things I Don't Use in Perl" created="2005-08-28 20:41:07" updated="2005-08-28 16:41:07">
<type id="11">
note</type>
<author id="246930">
fizbin</author>
<data>
<field name="doctext">
Maybe it's just that I was a math major, or got too close to those whacky Haskell nuts, but &lt;c&gt;map&lt;/c&gt; has always seemed a very natural metaphor for me.  (It's the natural extension of a function on a space A to the space A&lt;sup&gt;n&lt;/sup&gt;)&lt;p&gt;
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 &lt;c&gt;map&lt;/c&gt; doesn't seem natural:
&lt;code&gt;
my $i=0;
map {$keywordhash{$_}=(++$i);} @keywords;
&lt;/code&gt;
However, this useage seems quite natural:
&lt;code&gt;
%keywordhash = map {$_ =&gt; 1} @keywords;
&lt;/code&gt;
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
&lt;code&gt;
do {$keywordhash{$_}=(++$i);} for @keywords;
&lt;/code&gt;
over:
&lt;code&gt;
%keywordhash = map { $_ =&gt; ++$i } @keywords;
&lt;/code&gt;
Even though I like &lt;c&gt;map&lt;/c&gt;, because I'm not completely comfortable with the idea of depending on a certain execution order for &lt;c&gt;map&lt;/c&gt;, even though I know it has one.  When I'm thinking in a linear, this-gets-executed-then-this mode, &lt;c&gt;map&lt;/c&gt; rarely makes sense.  &lt;c&gt;map&lt;/c&gt; does however make sense as a higher-order function, and when I'm thinking like that it's quite natural.&lt;p&gt;
&lt;small&gt;(Of course, all style niceties get completely discarded inside JAPHs)&lt;/small&gt;
&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-246930"&gt;
--&lt;br&gt;
&lt;code&gt;@/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/;
map{y/X_/\n /;print}map{pop@$_}@/for@/&lt;/code&gt;
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
485945</field>
<field name="parent_node">
485950</field>
</data>
</node>
