Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ElvishJerricco/0bea7d7c394abd526afb46d4f980bce8 to your computer and use it in GitHub Desktop.
Save ElvishJerricco/0bea7d7c394abd526afb46d4f980bce8 to your computer and use it in GitHub Desktop.
newtype MonoidComp r m a = MonoidComp { unMonoidComp :: ContT r m a }
deriving (Functor, Applicative, Monad, MonadTrans)
instance (Applicative m, Monoid r) => Alternative (MonoidComp r m) where
empty = MonoidComp $ ContT $ const (pure mempty)
MonoidComp a <|> MonoidComp b =
MonoidComp $ ContT $ \f -> liftA2 (<>) (runContT a f) (runContT b f)
foldMapA :: (Foldable f, Applicative m, Monoid r) => (a -> m r) -> f a -> m r
foldMapA f = foldr (liftA2 (<>) . f) (pure mempty)
fromFoldable :: (Applicative m, Foldable f, Monoid r) => f a -> MonoidComp r m a
fromFoldable xs = MonoidComp $ ContT $ \f -> foldMapA f xs
runMonoidComp :: MonoidComp r m r -> m r
runMonoidComp = evalContT . unMonoidComp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment