On 02.11.2012 22:03, Graham Barr via RT wrote:
Show quoted text> <URL:
https://rt.cpan.org/Ticket/Display.html?id=76172 >
>
>
> On Nov 2, 2012, at 15:57 , Lukas Mai via RT wrote:
>>
>>
>>> We have had this discussion in the past, MJD was one who raised it, and it was settled then that sum(0, @list) is a sufficient compromise to support those that want a zero identity and those that want to use undef to detect that sum(@list) was passed an empty list
>>
>> How did you convince MJD? Because as far as I can tell, the matter has
>> been unsettled again.
>
> Just because YOU do not like it.
Yes. Is that not enough?
But apparently it's not just me, because in this bug alone there are
three people who think sum() should return 0 (as did MJD, apparently).
Show quoted text>> I don't find sum(0, @list) to be a sufficient
>> compromise because I'm skeptical that a compromise is needed in the
>> first place. What is the use case for sum() returning undef?
>
> Whats the use case for NULL in a relational database ?
Representing unknown or indeterminate values in columns marked as
allowing NULLs. For example, a table storing employees might have "birth
date" or "phone number" fields with NULL entries when the values are not
known.
What does that have to do with sum?
Show quoted text>>
>> Assuming we did change sum, why is @list ? sum(@list) : undef not a
>> sufficient compromise to get the other behavior?
>
> Why is sum(0, @list) not a sufficient compromise ?
Very funny. I gave a few reasons in the parts of my reply that you snipped.
Show quoted text> The field of programming is not always 100% mathematically correct. Sometimes things are added to make things easier
Yes, and now we want to do exactly that. It just so happens that the
thing we want to add to make things easier is also 100% mathematically
correct.
Show quoted text>> It more directly
>> expresses the idea of wanting to detect an empty list and returning
>> undef in that case.
>
> To me, a programmer not a mathematician sum(0, @list) more clearly expresses that I want zero as a default return.
I'm not a mathematician either, and I think the name "sum" already
implies that. If you wanted a different behavior, I'd suggest a name
like "sum_with_default" (or "sum_from" to indicate that the 0 is the
initial value of the accumulator).
Show quoted text>>
>> But that wasn't even the proposal. I just don't understand why you're so
>> against changing anything in List::Util ever.
>
> I am not, but I am against reducing functionality.
I don't understand. The sum0 proposal adds functionality and removes
nothing.
Show quoted text> The point of common libraries is to make code as useful as possible to as many people as possible.
> Having sum() return 0 reduces that IMO.
Yes, but I'm skeptical of your opinion. How do you know how many people
prefer what?
Besides, "as useful as possible to as many people as possible" can't be
our sole principle because blind application of it would lead us to an
incoherent mess (I'm thinking of PHP here (
http://php.net/gzgetss)).
Show quoted text> sum() is written is not broken as it does exactly what it is documented to do. It sounds like you
> just got surprised because you got an undef returned sometime. But thats documented, so IMO is correct.
I don't find this argument convincing. For example, consider a sqrt that
returns the string "nil" if the argument is 0. I would call that broken
because "nil" is obviously not the square root of anything, and sqrt has
no business returning a string anyway -- even if that behavior is
clearly documented, and even if some users actually want that behavior:
they can just write $x ? sqrt $x : "nil" instead, which is clearer to
anyone familiar with what sqrt normally does, I think.
To put it more bluntly: A bug doesn't cease to be a bug just because
it's documented. (I don't think this actually describes the sum issue -
I'm not accusing you of papering over a faulty implementation by
documenting the problems or anything.)
"not a bug - works as designed" is more convincing, but I'm arguing that
in this case the design could be improved.
Lukas