develooper Front page | perl.perl6.language | Postings from May 2005

Re: Plethora of operators

Thread Previous | Thread Next
Damian Conway
May 14, 2005 05:56
Re: Plethora of operators
Message ID:
Adam Kennedy wrote:

> And reduction? I write 25,000+ lines of 
> Perl a year, and if you are talking about something like 
> List::Util::reduce, I think I've used it maybe twice?

Which proves what? That you don't (yet) write the sort of code that benefits 
from reductions? That you don't (yet) think in terms of reductions, so you 
miss the many opportunities to use this handy metaoperation? Or perhaps just 
that reductions aren't (yet) easy enough to use that they naturally recommend 
themselves to you?

> But really, in what circumstances could someone possibly need reduction 
> so badly it needs to be in the core?

Well, I find myself importing it from List::Util quite frequently.

But that's beside the point. It doesn't need to be in the core because 
everyone will use it every day. It needs to be in the core because it needs to 
be fast and efficient and accessible to the optimizer.

And it ought to be in the core because it's a fundamental computing operation; 
one that many of us who write less code than you--but perhaps under a greater 
variety of paradigms--*do* use on a regular basis.

Here are a few of the things I'll be using reductions for in Perl 6...

     1. To add things up:

            $sum = [+] @amounts;

     2. To calculate the probability that I'll need to use a reduction today:

            $final_prob = [*] @independent_probs;

     3. To drill down a hierarchical data structure, following the path
        specified by a list of keys:

            $leaf_value = [.{}] %hash, @keys;

     4. To compute RMS values:

            $RMS = sqrt [+] @samples »** 2

     5. To take the dot product of two vectors:

            $dot_prod = [+] @vec1 »*« @vec2;

     6. As a cleaner form of C<< join.assuming(:sep<>) >>:

            $joined = [~] @strings;

     7. For parity checking a bitset:

            $parity = [?^] @bits;

     8. To verify the monotonicity of a sequence:

            $is_monotonic = [<] @numbers;

     9. To retrieve the first defined value in a list:

            $first_def = [//] @list;

     10. To apply a series of properties to a value:

            $propped = [but] $value, @properties;

Personally I think a metaoperator with that many uses is more than Swiss-Army 
enough to be in the core of Perl 6.


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About