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

Re: reduce metaoperator on an empty list

Thread Previous | Thread Next
Damian Conway
May 31, 2005 17:44
Re: reduce metaoperator on an empty list
Message ID:
All this discussion of "identity" defaults for reductions has been very 
interesting and enlightening.

But it seems to me that the simplest "correct" behaviour for reductions is:

     2+ args: interpolate specified operator
     1 arg:   return that arg
     0 args:  fail (i.e. thrown or unthrown exception depending on use fatal)

Then, as Brad previously reminded us all, to cover the possibility of empty 
arg lists, you just prepend the desired identity value:

     $sum  = [+] 0, @values;
     $prod = [*] 1, @values;
     $prob = [*] 0, @probs;

or else, as Stuart observed, postpend them as a default:

     $sum  = ([+] @values err 0);
     $prod = ([*] @values err 1);
     $prob = ([*] @probs  err 0);


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