semigroups 0.2.0 → 0.3.0
raw patch · 2 files changed
+54/−15 lines, 2 files
Files
- Data/Semigroup.hs +53/−14
- semigroups.cabal +1/−1
Data/Semigroup.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE DeriveDataTypeable #-} ----------------------------------------------------------------------------- -- | -- Module : Data.Semigroup@@ -11,24 +12,34 @@ ---------------------------------------------------------------------------- module Data.Semigroup ( Semigroup(..)+ -- * Semigroups , Min(..) , Max(..)- , Option(..)+ , First(..)+ , Last(..) , WrappedMonoid(..)+ -- * Monoids from Data.Monoid + , Dual(..)+ , Endo(..)+ , All(..)+ , Any(..)+ , Sum(..)+ , Product(..)+ -- * New Monoids+ , Option(..)+ , option ) where import Prelude hiding (foldr1)-import Data.Monoid-import Data.Foldable+import Data.Monoid hiding (First(..), Last(..))+import qualified Data.Monoid as Monoid+import Data.Data infixl 4 <> class Semigroup a where (<>) :: a -> a -> a - fold1 :: Foldable f => f a -> a- fold1 = foldr1 (<>) - -- Semigroups from Data.Monoid instance Semigroup a => Semigroup (Dual a) where@@ -49,28 +60,54 @@ instance Num a => Semigroup (Product a) where Product a <> Product b = Product (a * b) -instance Semigroup (First a) where- First Nothing <> b = b+instance Semigroup (Monoid.First a) where+ Monoid.First Nothing <> b = b a <> _ = a -instance Semigroup (Last a) where- a <> Last Nothing = a+instance Semigroup (Monoid.Last a) where+ a <> Monoid.Last Nothing = a _ <> b = b +-- * Other Semigroups newtype Min a = Min { getMin :: a } + deriving (Eq,Ord,Bounded,Show,Read,Data,Typeable)+ instance Ord a => Semigroup (Min a) where Min a <> Min b = Min (a `min` b) +instance (Ord a, Bounded a) => Monoid (Min a) where+ mempty = maxBound+ mappend = (<>) + newtype Max a = Max { getMax :: a } + deriving (Eq,Ord,Bounded,Show,Read,Data,Typeable)+ instance Ord a => Semigroup (Max a) where Max a <> Max b = Max (a `min` b) --- (==)/XNOR on Bool forms a Semigroup, but has no good name+instance (Ord a, Bounded a) => Monoid (Max a) where+ mempty = minBound+ mappend = (<>) +-- | Use Option (First a) -- to get the behavior of Data.Monoid.First+newtype First a = First { getFirst :: a }+ deriving (Eq,Ord,Bounded,Show,Read,Data,Typeable) +instance Semigroup (First a) where+ a <> _ = a++-- | Use Option (Last a) -- to get the behavior of Data.Monoid.Last+newtype Last a = Last { getLast :: a }+ deriving (Eq,Ord,Bounded,Show,Read,Data,Typeable)++instance Semigroup (Last a) where+ _ <> b = b++-- (==)/XNOR on Bool forms a Semigroup, but has no good name+ newtype WrappedMonoid m = WrapMonoid { unwrapMonoid :: m } - deriving (Show, Read, Eq, Ord)+ deriving (Data, Typeable, Show, Read, Eq, Ord) instance Monoid m => Semigroup (WrappedMonoid m) where WrapMonoid a <> WrapMonoid b = WrapMonoid (a `mappend` b)@@ -79,9 +116,11 @@ mempty = WrapMonoid mempty WrapMonoid a `mappend` WrapMonoid b = WrapMonoid (a `mappend` b) - newtype Option a = Option { getOption :: Maybe a } - deriving (Show, Read, Eq, Ord)+ deriving (Data, Typeable, Show, Read, Eq, Ord)++option :: b -> (a -> b) -> Option a -> b+option n j (Option m) = maybe n j m instance Semigroup a => Semigroup (Option a) where Option Nothing <> b = b
semigroups.cabal view
@@ -1,6 +1,6 @@ name: semigroups category: Control, Comonads-version: 0.2.0+version: 0.3.0 license: BSD3 cabal-version: >= 1.2 license-file: LICENSE