"fold": a replacement for "reduce"

monarch_dodra monarchdodra at gmail.com
Tue Mar 25 14:08:11 PDT 2014


On Tuesday, 25 March 2014 at 20:37:31 UTC, Nordlöw wrote:
> Correction:
>
>> I guess typeof return value could be deduced according + and * 
>> at least.
>
> Return type is of course obvious, value is not...

The return type is not actually obvious. You'd think:
alias S = typeof(fun(r.front, r.front));
S s;

Which is a good start. But then
is (S == typeof(fun(s, r.front))) ?

You have no guarantees that you'll have "stability" of the 
returned type.
More often than not, it *is* stable, but there are cases where it 
is not.

This is not a huge problem, because the consequence is simply 
that it fails to compile. We can simply catch it, and tell the 
user to provide an explicit seed, so as to help out the function.

But as you said, for the value, the is simply no correct initial 
value, unless you know the "identity" value of your operator. But 
even then, that value makes no sense is the range is empty.


More information about the Digitalmars-d mailing list