Created
June 6, 2012 00:28
-
-
Save lambda-fairy/2879088 to your computer and use it in GitHub Desktop.
Set options temporarily - useful with Network.HTTP.Conduit.Browser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import qualified Control.Exception.Lifted as Lifted | |
import Control.Monad.Trans.Control | |
-- | Set an option, run an action, then restore the option to its | |
-- previous value. | |
-- | |
-- @saveRestore get set value action@ is roughly equivalent to: | |
-- | |
-- > do orig <- get | |
-- > set value | |
-- > action | |
-- > set orig | |
-- | |
-- except with proper exception handling. | |
saveRestore | |
:: MonadBaseControl IO m | |
=> m a -- ^ get value | |
-> (a -> m ()) -- ^ set value | |
-> a -- ^ value to set | |
-> m r -- ^ action to run in between | |
-> m r | |
saveRestore get set value action = Lifted.bracket open close between | |
where | |
open = do | |
orig <- get | |
set value | |
return orig | |
close = set | |
between _ = action |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment