0

I have a list of some ~10.000 observations in a custom data type observations :: [Obs] that I want to aggregate into a statistics object :: Stat . A fold seemed like a sensible choice. I find myself with two options.

(1) Define a function accumulate :: Stat -> Obs -> Stat that I then left fold (strictly). foldl' accumulate acc0 observations

But my statistics object only contain accumulators such as Count and Sum, so there should be a monoidal structure. That gives me one more option

(2) define a convert :: Obs -> Stat and use foldMap from Data.Foldable likefoldMap convert observations.

my question What reasons are there for going the monoidal way? Performance? Maintainability? Other?

LudvigH
  • 3,662
  • 5
  • 31
  • 49
  • 2
    FWIW I think using foldMap makes it clearer what your intent is, and makes it harder to screw up. foldl is sufficiently flexible that you could make a mistake in your folding function, and it obscures your intent to just map-then-aggregate: other humans have to read your fold function carefully to realize it's just doing what foldMap does. – amalloy Jul 13 '17 at 19:23
  • @amalloy it is an important reason. I think I'll go with that to begin with, and switch to a strict fold later if needed. – LudvigH Jul 13 '17 at 19:25

0 Answers0