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

Re: reduce metaoperator on an empty list

Thread Previous | Thread Next
Deborah Pickett
June 1, 2005 02:10
Re: reduce metaoperator on an empty list
Message ID:
On Wed, 1 Jun 2005 13.19, Joe Gottman wrote:
> > Juerd asked:
> > >>    2+ args: interpolate specified operator
> > >>    1 arg:   return that arg
> > >>    0 args:  fail (i.e. thrown or unthrown exception depending on use
> > fatal)
> >
> > > Following this logic, does join(" ", @foo) with +@foo being 0 fail too?
> > No. It returns empty string. You could think of C<join> as being
> > implemented:
> >      sub join (Str $sep, *@list) { reduce { $^a ~ $sep ~ $^b } "", @list
> > }
> >
> > Just as C<sum> is probably implemented:
> >
> >      sub sum (*@list) { [+] 0, @list }
>    If this were the case, then
> 	join '~', 'a', 'b', 'c'
>      would equal '~a~b~c' instead of 'a~b~c'

Another broken symmetry.  Ah, Perl 5's full of them, and I see that Perl 6 
will be no exception.  Excuse me while I find my cynic hat . . ah, here it 

I'm still in the camp of those wanting each operator to know its own identity 
value (perhaps in terms of a trait).  The identity of multiplication (say) is 
always 1, after all, and it doesn't change depending on when you do 
multiplication in your own code.  In mathematical terms, it's a property of 
the operator, not of how it's used.

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 

Multiplication's an easy example, in any case, and any programmer will know 
the identity is 1.  But look at the confusion here on this list among people 
over the identities for <, >, ** and other beasts.  I've already forgotten 
some of them.

(On another note: I had a thought the other day: [op] produces one thing from 
a list, and »op« produces a list from a list.  When I squint with hindsight, 
it makes more sense to me for »op« to be the one that converges a list to a 
single point, and [op] to do listy things to lists.  I'm not seriously 
suggesting a switch - there's probably parsing issues with the thought - I'm 
just sayin', is all.)

Debbie Pickett

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