"fold": a replacement for "reduce"

Simen Kjærås simen.kjaras at gmail.com
Thu Mar 27 05:47:42 PDT 2014


On 2014-03-25 17:22, monarch_dodra wrote:

> I'm taking this naming-changing event as an opportunity to "cleanup"
> reduce too. One thing that gets on my nerves is that "range.reduce()" is
> not nothrow, because it throws an exception if the range is empty.
>
> I think this is wrong. popping an empty range is an *Error*, and should
> be validated by the programmer. Because of this, it is currently not
> possible to use "reduce(range)" in nothrow context.
>
> This however, even with a name change, it *is* change of behavior. Do
> you feel this is acceptable? Do you want this change at all? Or do you
> think an Exception is fine?

Your new fold (foldl? Should we have foldr as well?) should be nothrow. 
As for updating reduce, I'm slightly in favor of making it nothrow as 
well, but is this really necessary? The cases where it's already used 
will gain nothing from it, and new code would use fold instead. Or do I 
misunderstand?

Even with that argument though, I'd say make it nothrow. Like Meta said, 
it's probably remnants from elder times.

--
   Simen


More information about the Digitalmars-d mailing list