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

Re: reduce metaoperator on an empty list

Thread Previous | Thread Next
From:
Damian Conway
Date:
June 8, 2005 00:54
Subject:
Re: reduce metaoperator on an empty list
Message ID:
42A6A424.5070704@conway.org
Larry wrote:

> Okay, I've made up my mind.  The "err" option is not tenable because
> it can cloak real exceptions, and having multiple versions of reduce is
> simply multiplying entities without adding much power.  So let's allow
> an optional "identvalue" trait on operators.  If it's there, reduce
> can use it.  If it's not, reduce returns failure on 0 args.

So, just to clarify, for the expression:

	[op] @list

if C<op> has an C<:identval> trait then the result is:

	* &op.identval()                            if @list == 0
         * @list[0]                                  if @list == 1
         * @list[0] op @list[1] op...op @list[-1]    if @list > 1

Otherwise the result is:

	* fail                                      if @list == 0
         * @list[0]                                  if @list == 1
         * @list[0] op @list[1] op...op @list[-1]    if @list > 1

Correct?
	

> But I would like to stress that this is an *optional* property.
> We will only define an identity value for those operators for
> which it is obvious what an "accumulator" should be initialized to.
> (This is probably also the set of operators for which op= doesn't warn
> when the left side is undefined, so "identval" is doing double duty.

So, to clarify again, if $var is undefined, then the assignment:

	$var op= $value;

is equivalent to:

	$var = (&op.does(identval) ?? &op.identval() :: undef) op $value;

Correct?


> In Perl 5 we hardwired that, so it'd be nice to generalize the concept
> in Perl 6.)

Indeed.


> In fact, by the accumulator argument, maybe it should be called
> "initvalue" rather than "identvalue".

I'd be very leery of calling it "initvalue", since people are apt to confuse it 
with real initialization. I'd be inclined to call it "identityval" and simply 
say that an undefined variable that is op-assigned defaults to the identity 
value of the operator (if any).

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