mirror of
https://github.com/haskell-effectful/effectful.git
synced 2024-11-28 00:09:30 +03:00
Add withReader (#68)
Fixes https://github.com/haskell-effectful/effectful/issues/9.
This commit is contained in:
parent
fb2ec9a873
commit
d74ae52e10
@ -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
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user