Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hoistDeclare #22

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Data/OpenApi/Declare.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE RankNTypes #-}
-- |
-- Module: Data.OpenApi.Declare
-- Maintainer: Nickolay Kudasov <nickolay@getshoptv.com>
Expand All @@ -28,7 +29,10 @@ import Control.Monad.Trans.RWS.Lazy as Lazy
import Control.Monad.Trans.RWS.Strict as Strict
import Control.Monad.Trans.Writer.Lazy as Lazy
import Control.Monad.Trans.Writer.Strict as Strict
import Control.Lens.Lens (Lens')
import Data.Functor.Identity
import Data.Functor.Compose
import Data.Tuple (swap)

-- | A declare monad transformer parametrized by:
--
Expand Down Expand Up @@ -115,6 +119,14 @@ execDeclareT (DeclareT f) d = fst <$> f d
undeclareT :: (Monad m, Monoid d) => DeclareT d m a -> m a
undeclareT = flip evalDeclareT mempty

-- | Lift a declaration-producing computation for a subpart into a
-- declaration-producing computation for the whole.
hoistDeclareT :: Functor m => Lens' d d' -> DeclareT d' m a -> DeclareT d m a
hoistDeclareT l = DeclareT . unconvert . l . convert . runDeclareT
where
convert f = Compose . fmap swap . f
unconvert f = fmap swap . getCompose . f

-- | A declare monad parametrized by @d@ — the output to accumulate (declarations).
--
-- This monad is similar to both state and writer monads.
Expand Down