"fold": a replacement for "reduce"

Brian Rogoff brogoff at gmail.com
Thu Mar 27 09:01:43 PDT 2014


On Thursday, 27 March 2014 at 14:45:05 UTC, monarch_dodra wrote:
> On Thursday, 27 March 2014 at 14:41:19 UTC, Brian Rogoff wrote:
>> On Thursday, 27 March 2014 at 13:23:27 UTC, monarch_dodra 
>> wrote:
>>> On Thursday, 27 March 2014 at 12:48:41 UTC, Simen Kjærås 
>>> wrote:
>>>> Your new fold (foldl? Should we have foldr as well?)
>>>
>>> "fold" (from what I understood) is what you call "foldl". It 
>>> was discussed to not introduce "foldr", as it's just 
>>> "fold!(binaryReverseArgs!Fun)(range.retro);".
>>>
>>> I'm not sure I was able to understand what the difference 
>>> between "fold" and "foldl" is...
>>
>> In functional languages,
>>
>> fold_left(f, a, [b1, ..., bn]) is
>> f((... (f, (f, a, b1), b2) ...), bn)
>>
>> as you can see, the innermost f call is on the leftmost 
>> sequence element, and
>>
>> fold_right(f, [a1, ..., an], b) is
>> f(a1, (f a2 (... f(an, b) ...)))
>>
>> That's how I think of them.
>
> Right, but what about "fold" vs "fold_left"? Is there a 
> difference?

There's just fold_left and fold_right, or foldl and foldr if you 
prefer, though if f is associative they're both the same.




More information about the Digitalmars-d mailing list