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

Re: reduce metaoperator on an empty list

Thread Previous | Thread Next
From:
Damian Conway
Date:
May 31, 2005 17:44
Subject:
Re: reduce metaoperator on an empty list
Message ID:
429D04FF.8000800@conway.org
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);


Damian

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About