2 Dec 18:57
Proposal: Max and Min for Monoid (ticket # 1952)
From: Conal Elliott <conal <at> conal.net>
Subject: Proposal: Max and Min for Monoid (ticket # 1952)
Newsgroups: gmane.comp.lang.haskell.libraries
Date: 2007-12-02 17:57:50 GMT
Subject: Proposal: Max and Min for Monoid (ticket # 1952)
Newsgroups: gmane.comp.lang.haskell.libraries
Date: 2007-12-02 17:57:50 GMT
I'd like to add two instances to Data.Monoid, alongside of All/Any, Sum/Product, and First/Last.
Here's a current instance (as a style example):
-- | Boolean monoid under conjunction.newtype All = All { getAll :: Bool }
deriving (Eq, Ord, Read, Show, Bounded)
instance Monoid All where
mempty = All True
All x `mappend` All y = All (x && y)
My proposed addition:
-- | Ordered monoid under 'max'.newtype Max a = Max { getMax :: a }
deriving (Eq, Ord, Read, Show, Bounded)
instance (Ord a, Bounded a) => Monoid (Max a) where
mempty = Max minBound
Max a `mappend` Max b = Max (a `max` b)
-- | Ordered monoid under 'min'.
newtype Min a = Min { getMin :: a }
deriving (Eq, Ord, Read, Show, Bounded)
instance (Ord a, Bounded a) => Monoid (Min a) where
mempty = Min minBound
Min a `mappend` Min b = Min (a `min` b)
I have a niggling uncertainty about the Ord & Bounded instances for Min a? Is there a reason flip the a ordering instead of preserving it?
Suggested review period: two weeks (ending December 16).
Comments?
- Conal
_______________________________________________ Libraries mailing list Libraries <at> haskell.org http://www.haskell.org/mailman/listinfo/libraries
RSS Feed