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 20:14
Subject:
Re: reduce metaoperator on an empty list
Message ID:
429D27F7.6050609@conway.org
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 }


> I dislike this, and would prefer [op] with no elements to simply return
> whatever () returns: an empty list in list context, undef in scalar
> context.

I'd have assumed that "empty list in list context, undef in scalar
context" *is* what C<fail> returns when C<use fatal> isn't in effect.

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