Skip to content

Instantly share code, notes, and snippets.

@lambda-fairy
Created June 6, 2012 00:28
Show Gist options
  • Save lambda-fairy/2879088 to your computer and use it in GitHub Desktop.
Save lambda-fairy/2879088 to your computer and use it in GitHub Desktop.
Set options temporarily - useful with Network.HTTP.Conduit.Browser
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