Damian Conway wrote:
Deborah Pickett wrote:

You are going to see empty lists more often than you think in expressions like
  $product = [*] @array;
and having to write that as
  $product = [*] 1, @array;
just to protect against a common case doesn't exactly flaunt Perl's DWIMmery to me. I *have* to write 1 there, or otherwise the reduce meta-operator isn't calculating the product when there are items in @array. This hardly makes sense from a Huffman perspective. Someone please convince me otherwise.


The problem is that writing 1 there is still wrong in the "no arguments" case. The product of zero numbers cannot possibly be one in any common sense interpretation. (And, yes, I'm perfectly well aware of the mathematical interpretations in which it does make sense...that's not the point.)

Sure it is: it's the number you have before you've multiplied any numbers into it. It's the center of the world, for multiplication. If you started anywhere else you wouldn't end up at the correct product. If you started at zero you'd never get off the dime.


What you want is:

    $product = ([*] @values err 0);

Not what I'd ever want...


Or:

    $factorial = ([*] 1..$n err 1);

So what you want is not an identity value as default (which isn't even possible for many operators, as Luke pointed out), but a predictable failure value as default, so you can intercept that failure and choose your own outcome in the edge case.

Damian

This is why I would rather the o -> [o] circumfixion left [o] an infix, not prefix operator. I would rather be explicit about my identity:

        $product = 1 [*] @array;

        $factorial = 1 [*] 1..$n;

        $text = "" [~] @lines;

        $balance = $balance [-] @debits;
or
        $balance [-]= @debits;

which last suggests how you don't always want to start at the identity, and how [-] makes sense in this context.


Unfortunately it doesn't handle the case of join $sep, @strings:

        "" [{$^a ~ $sep ~ $^b}] @strings

(yes, I know that's not going to pass lexical analysis) since, as was pointed out, you get an extra $sep at the front.


yours,
Roger Hale

Reply via email to