reduce -> fold?

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Wed Feb 3 08:39:26 PST 2016


On 02/01/2016 03:46 AM, Dragos Carp wrote:
> On Friday, 29 January 2016 at 20:40:18 UTC, Andrei Alexandrescu wrote:
>> That'd be interesting if (a) lazy and (b) general a la
>> https://dlang.org/library/std/range/recurrence.html. -- Andrei
>
> To be clear, by general you mean to allow functions with more than 2
> arguments?

My ambitions were lower :o). I was thinking of supporting any operation, 
not only summation.

> For example if you have:
>
> foo(int i, int j, int k) { return i + j + k; }
>
> then:
>
> scan!foo([1, 2, 3, 4]).array returns [1, 2, 6, 12]
>
> Is "scan" (thanks Timon) telling enough? The python "accumulate"
> conflicts with the C++ meaning.

That's a sliding window of compile-time-known size, which is interesting 
on its own. There are several ways to handle the limits, each useful in 
certain situations. I don't get where 12 comes from in your example.


Andrei



More information about the Digitalmars-d mailing list