Skip to content

Commit ec835cb

Browse files
committed
Add hoistDeclare
1 parent 26d8def commit ec835cb

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/Data/OpenApi/Declare.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import Control.Monad.Trans.RWS.Strict as Strict
2929
import Control.Monad.Trans.Writer.Lazy as Lazy
3030
import Control.Monad.Trans.Writer.Strict as Strict
3131
import Data.Functor.Identity
32+
import Data.Functor.Compose
33+
import Data.Tuple (swap)
3234

3335
-- | A declare monad transformer parametrized by:
3436
--
@@ -115,6 +117,14 @@ execDeclareT (DeclareT f) d = fst <$> f d
115117
undeclareT :: (Monad m, Monoid d) => DeclareT d m a -> m a
116118
undeclareT = flip evalDeclareT mempty
117119

120+
-- | Lift a declaration-producing computation for a subpart into a
121+
-- declaration-producing computation for the whole.
122+
hoistDeclareT :: Functor m => Lens' d d' -> DeclareT d' m a -> DeclareT d m a
123+
hoistDeclareT l = DeclareT . unconvert . l . convert . runDeclareT
124+
where
125+
convert f = Compose . fmap swap . f
126+
unconvert f = fmap swap . getCompose . f
127+
118128
-- | A declare monad parametrized by @d@ — the output to accumulate (declarations).
119129
--
120130
-- This monad is similar to both state and writer monads.

0 commit comments

Comments
 (0)