In recent golfs and other ponderings, it's quite common to run into expression such as:
my $i; for (0..10) { $i += $_; }
That is, you want to apply the same expression over a list of values, and return a value from that. I'll call this a "list-to-scalar" mapping, in much the same that map and grep are both "list-to-list" mappings.

I am wondering if someone has taken the time to write a generic list-to-scalar function that works similar to map. For example, assume this function is called operate (I'm sure the name could be better). Then this:

my $sum = operate { $~ + $_ } (0..10);
would be equivalent to the above. "$~" is a local variable that is initially set to null at the start of the loop, and is set to the value of the code block after that block is executed for each item. (I can't recall of any current use of $~, and IIRC, it is a 'reserved' variable).

I know this might seem trivial, as you're simply removing an explicit my statement. But providing such a function seems to give perl a bit more symmetry when you come it with map and grep. (And as recent discussions have shown, it's better to think of perl as a list-oriented language rather than scalar). This also makes some other functions a bit more obvious:

#Max: my $max = operate { $~ > $_ ? $~ : $_ } @list; #Straight Concat: my $string = operate { $~.$_ } @list; #Count occurences of words in a file: my $count = operate { $~ + /the/ } <FILE>;
Writing such a function shouldn't be impossible, as it's compariable to treemap and other code here at PM. But before I tackle this, I want to verify I'm not reinventing the wheel, nor running into any trouble with the usage I'm suggesting.

Update - As shown below, the functionality I was looking for is sufficiently taken care of by the reduce fuction in List::Util.


Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

In reply to List-to-Scalar Function? by Masem

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.