diff --git a/effectful-core/src/Effectful/Reader/Dynamic.hs b/effectful-core/src/Effectful/Reader/Dynamic.hs index 2706ab3..a030684 100644 --- a/effectful-core/src/Effectful/Reader/Dynamic.hs +++ b/effectful-core/src/Effectful/Reader/Dynamic.hs @@ -9,6 +9,7 @@ module Effectful.Reader.Dynamic -- ** Handlers , runReader + , withReader -- ** Operations , ask @@ -36,6 +37,17 @@ runReader r = reinterpret (R.runReader r) $ \env -> \case Ask -> R.ask Local f m -> localSeqUnlift env $ \unlift -> R.local f (unlift m) +-- | Execute a computation in a modified environment. +withReader + :: (r1 -> r2) + -- ^ The function to modify the environment. + -> Eff (Reader r2 : es) a + -- ^ Computation to run in the modified environment. + -> Eff (Reader r1 : es) a +withReader f m = do + r <- ask + raise $ runReader (f r) m + ---------------------------------------- -- Operations diff --git a/effectful-core/src/Effectful/Reader/Static.hs b/effectful-core/src/Effectful/Reader/Static.hs index 2957d9e..98df9af 100644 --- a/effectful-core/src/Effectful/Reader/Static.hs +++ b/effectful-core/src/Effectful/Reader/Static.hs @@ -5,6 +5,7 @@ module Effectful.Reader.Static -- ** Handlers , runReader + , withReader -- ** Operations , ask @@ -29,6 +30,17 @@ runReader -> Eff es a runReader r = evalStaticRep (Reader r) +-- | Execute a computation in a modified environment. +withReader + :: (r1 -> r2) + -- ^ The function to modify the environment. + -> Eff (Reader r2 : es) a + -- ^ Computation to run in the modified environment. + -> Eff (Reader r1 : es) a +withReader f m = do + r <- ask + raise $ runReader (f r) m + -- | Fetch the value of the environment. ask :: Reader r :> es => Eff es r ask = do