diff options
Diffstat (limited to 'lib/ControlMaybe.hs')
-rw-r--r-- | lib/ControlMaybe.hs | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/lib/ControlMaybe.hs b/lib/ControlMaybe.hs new file mode 100644 index 0000000..659dab7 --- /dev/null +++ b/lib/ControlMaybe.hs | |||
@@ -0,0 +1,29 @@ | |||
1 | {-# LANGUAGE ScopedTypeVariables #-} | ||
2 | module ControlMaybe where | ||
3 | |||
4 | -- import GHC.IO.Exception (IOException(..)) | ||
5 | import Control.Exception as Exception (IOException(..),catch) | ||
6 | |||
7 | |||
8 | withJust :: Monad m => Maybe x -> (x -> m ()) -> m () | ||
9 | withJust (Just x) f = f x | ||
10 | withJust Nothing f = return () | ||
11 | |||
12 | whenJust :: Monad m => m (Maybe x) -> (x -> m ()) -> m () | ||
13 | whenJust acn f = do | ||
14 | x <- acn | ||
15 | withJust x f | ||
16 | |||
17 | |||
18 | catchIO_ :: IO a -> IO a -> IO a | ||
19 | catchIO_ a h = Exception.catch a (\(_ :: IOException) -> h) | ||
20 | |||
21 | catchIO :: IO a -> (IOException -> IO a) -> IO a | ||
22 | catchIO body handler = Exception.catch body handler | ||
23 | |||
24 | handleIO_ :: IO a -> IO a -> IO a | ||
25 | handleIO_ = flip catchIO_ | ||
26 | |||
27 | |||
28 | handleIO :: (IOException -> IO a) -> IO a -> IO a | ||
29 | handleIO = flip catchIO | ||