reduce -> fold?

Dragos Carp via Digitalmars-d digitalmars-d at puremagic.com
Thu Feb 4 00:29:46 PST 2016


On Wednesday, 3 February 2016 at 16:39:26 UTC, Andrei 
Alexandrescu wrote:
> 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.
>

Good that I asked. Contrary to "reduce", "recurrence" works also 
with functions with more than 2 arguments, so I saw it as a hint 
in this direction.

>> 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.

I calculated Yn = fct(Yn-2, Yn-1, Xn) thus Y3 = 2 + 6 + 4 == 12

I will prepare a PR for the binary function implementation.



More information about the Digitalmars-d mailing list