composewell / streamly

High performance, concurrent functional programming abstractions
https://streamly.composewell.com
Other
856 stars 64 forks source link

Handle saturation/overflow in numeric folds #779

Open harendra-kumar opened 3 years ago

harendra-kumar commented 3 years ago

Data.Fold.sum

We should probably use a Bounded a constraint and make sum terminate if it overflows beyond the bound. Currently overflows give wrong sum silently.

The currentsum can be renamed to sumUnsafe, and can be used if it gives noticeable perf advantage in perf sensitive code.

For Integer type (unbounded) we can have sumInteger. For (Num a, Floating a) we can have a sumFloating. Or we can create an Unbounded type class for such types? And then have a sumUnbounded. To begin with we can just use sumUnsafe for these use cases and leave this as a TODO.

Data.Fold.product

Overflow applies to product as well in the same way as sum.

Data.Fold.minimum (maximum/minimumBy/maximumBy)

minimum/maximum and minimumBy/maximumBy would terminate if the value reached the min/max possible value for the type.

adithyaov commented 2 years ago

Pushing this to the next milestone. This looks like a breaking change.