![]() |
|
go ahead... be a heretic | |
PerlMonks |
List-to-Scalar Function?by Masem (Monsignor) |
on May 09, 2001 at 02:08 UTC ( [id://78977]=perlmeditation: print w/replies, xml ) | Need Help?? |
In recent golfs and other ponderings, it's quite common to run into expression such as:
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: 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: 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
Back to
Meditations
|
|