Skip to content

Commit

Permalink
Add hoistDeclare
Browse files Browse the repository at this point in the history
  • Loading branch information
masaeedu committed Apr 23, 2021
1 parent 26d8def commit d928083
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Data/OpenApi/Declare.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ 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 Data.Functor.Identity
import Data.Functor.Compose
import Data.Tuple (swap)

-- | A declare monad transformer parametrized by:
--
Expand Down Expand Up @@ -115,6 +117,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

0 comments on commit d928083

Please sign in to comment.