Say I have a map with measurable objects, and I'd like the maximum of their widths, defaulting to 0. The machinery of Foldable
(foldMap
) and Semigroup
(Max
) seems perfect, except that I can't seem to introduce the arbitrary lower bound.
data Thing
width :: Thing -> Double
maxWidth :: Map k Thing -> Double
maxWidth things = getMax . foldMap (Max . width) $ things
This rightly complains about the missing Bounded
instance for Double
, since the Monoid
instance of Max a
uses mempty = minBound
.
I see that the source for Data.Foldable
uses a different definition of newtype Max
to implement maximum
. That variant would do nicely, but it seems to not be exported:
maxWidth things = fromMaybe 0 . getMax . foldMap (Max . Just . width) $ things