diff --git a/src/Data/Monoid/Extra.hs b/src/Data/Monoid/Extra.hs index 8f1a763..d04e1b3 100644 --- a/src/Data/Monoid/Extra.hs +++ b/src/Data/Monoid/Extra.hs @@ -3,10 +3,14 @@ module Data.Monoid.Extra( module Data.Monoid, -- * Extra operations - mwhen + mwhen, + + -- * Kleisli endomorphisms + KEndo(KEndo, appKEndo) ) where import Data.Monoid +import Control.Monad -- | Like 'Control.Monad.when', but operating on a 'Monoid'. If the first argument -- is 'True' returns the second, otherwise returns 'mempty'. @@ -15,3 +19,16 @@ import Data.Monoid -- > mwhen False "test" == "" mwhen :: Monoid a => Bool -> a -> a mwhen b x = if b then x else mempty + +-- | The intersection of 'Data.Monoid.Endo' and 'Control.Arrow.Kleisli'. This +-- type provides a 'Monoid' instance for composition of monadic actions +-- @a -> m a@. +newtype KEndo m a = KEndo { appKEndo :: a -> m a } + +instance (Monad m) => Semigroup (KEndo m a) where + -- | Right-to-left composition + KEndo f <> KEndo g = KEndo (f <=< g) + +instance (Monad m) => Monoid (KEndo m a) where + mempty = KEndo pure +