reduce -> fold?

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Thu Feb 4 12:45:21 PST 2016


On 02/04/2016 10:38 AM, Atila Neves wrote:
> On Wednesday, 3 February 2016 at 21:45:04 UTC, Timon Gehr wrote:
>> On 02/03/2016 09:12 PM, Atila Neves wrote:
>>>
>>> https://github.com/D-Programming-Language/phobos/pull/3968
>>>
>>> I think fold should be nothrow, but maybe that's just me. It's also a
>>> massive pain to make it that way, so I didn't for now.
>>
>> Returning Unqual!(ElementType!R).init makes no sense though.
>> The "correct" result of fold!f([]) is a (often, the) value 'a' such
>> that for any 'b', 'f(a,b)==b' (which is the canonical choice of
>> "seed"), but there is no way for fold to infer such a value.
>
> Right. I wrote in a hurry and without checking the code I'd written
> yesterday. The correct value to return for one function would be:
>
> template fold(fun...) {
>      alias binFuncs = staticMap!(binaryFun, fun);
>      // checks for fun.length == 1, etc.
>      auto fold(R)(R r) {
>          return Unqual!(typeof(binFuncs[0](r.front, r.front))).init;
>      }
> }

My point was more that the .init value is often not the value you want.


More information about the Digitalmars-d mailing list