Add withReader (#68)

Fixes https://github.com/haskell-effectful/effectful/issues/9.
This commit is contained in:
Andrzej Rybczak 2022-07-17 17:58:59 +02:00 committed by GitHub
parent fb2ec9a873
commit d74ae52e10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -9,6 +9,7 @@ module Effectful.Reader.Dynamic
-- ** Handlers -- ** Handlers
, runReader , runReader
, withReader
-- ** Operations -- ** Operations
, ask , ask
@ -36,6 +37,17 @@ runReader r = reinterpret (R.runReader r) $ \env -> \case
Ask -> R.ask Ask -> R.ask
Local f m -> localSeqUnlift env $ \unlift -> R.local f (unlift m) 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 -- Operations

View File

@ -5,6 +5,7 @@ module Effectful.Reader.Static
-- ** Handlers -- ** Handlers
, runReader , runReader
, withReader
-- ** Operations -- ** Operations
, ask , ask
@ -29,6 +30,17 @@ runReader
-> Eff es a -> Eff es a
runReader r = evalStaticRep (Reader r) 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. -- | Fetch the value of the environment.
ask :: Reader r :> es => Eff es r ask :: Reader r :> es => Eff es r
ask = do ask = do