"fold": a replacement for "reduce"

"Nordlöw" per.nordlow at gmail.com
Tue Mar 25 13:36:04 PDT 2014


On Tuesday, 25 March 2014 at 17:22:45 UTC, monarch_dodra wrote:
> I'm working on something called "fold". It is designed as 
> nothing more than a replacement for "reduce", but where the 
> seed comes *second* in terms of arguments. It allows this:
>
> someLongUFCSChain().reduce(intoThis);

Good! I've been greatly disturbed by this too.

Especially by the fact that reduce throws upon empty input 
because it currently has no way of deducing return value. This 
can be a greatly annoying "surprise" to new users.

 From algebra we learn the following relations:

Operator Unit
+,(-)    0
*,(/)    1
min      T.max
max      T.min
..

I guess typeof return value could be deduced according + and * at 
least.

Have you perhaps found a way to deduce it in the general case 
using the reduce function and ElementType of input range, 
something like

     typeof(binaryFun(E, E))

where

     alias E = ElementType!R,

I thought about adding an overload for reduce using template 
restriction that would solve the problem without adding a new 
function. But I guess that adding such an overload could cause 
just as much confusion to programmers who are used to the current 
syntax.


More information about the Digitalmars-d mailing list