hanshoglund / average

Provides a (fake) monoid for calculating arithmetic means.
BSD 3-Clause "New" or "Revised" License
1 stars 4 forks source link

Refactor: store only a weight and running sum #5

Open leftaroundabout opened 7 years ago

leftaroundabout commented 7 years ago

Re #4:

The list based monoid representation is not space efficient. This implementation also leads towards a number of confusing and highly opinionated instances. This new implementation is space efficient, ideal for on-line algorithms and does not include possibly confusing/problematic instances.

Unfortunately, storing a running sum means we can't just fmap or combine values anymore, because the stored values are magnified when averaged. I adressed this issue by changing from Functor to MonoFunctor, and performing the average before those operations.

It would also be possible to just store the average right away; I've explored that in https://github.com/leftaroundabout/average/tree/refactor/weight-and-average. However that approach makes the Monoid instance quite inefficient, and we should probably prioritise keeping that efficient because it's the centerpiece of this package.