summaryrefslogtreecommitdiff
path: root/server/src/ControlMaybe.hs
blob: a101d667c2633078bd0651e4f9281bb64f278ec4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
{-# LANGUAGE CPP                 #-}
{-# LANGUAGE ScopedTypeVariables #-}
module ControlMaybe
   ( module ControlMaybe
   , module Data.Functor
   ) where

-- import GHC.IO.Exception (IOException(..))
import Control.Monad
import Data.Functor
import System.IO.Error


-- forM_ with less polymorphism.
withJust :: Monad m => Maybe x -> (x -> m ()) -> m ()
withJust m f = forM_ m f
{-# INLINE withJust #-}

whenJust :: Monad m => m (Maybe x) -> (x -> m ()) -> m ()
whenJust acn f = acn >>= mapM_ f
{-# INLINE whenJust #-}


catchIO_ :: IO a -> IO a -> IO a
catchIO_ body catcher = catchIOError body (\_ -> catcher)
{-# INLINE catchIO_ #-}

handleIO_ :: IO a -> IO a -> IO a
handleIO_ catcher body = catchIOError body (\_ -> catcher)
{-# INLINE handleIO_ #-}


handleIO :: (IOError -> IO a) -> IO a -> IO a
handleIO catcher body = catchIOError body catcher
{-# INLINE handleIO #-}

#if !MIN_VERSION_base(4,11,0)
-- | Flipped version of '<$>'.
--
-- @
-- ('<&>') = 'flip' 'fmap'
-- @
--
-- @since 4.11.0.0
--
-- ==== __Examples__
-- Apply @(+1)@ to a list, a 'Data.Maybe.Just' and a 'Data.Either.Right':
--
-- >>> Just 2 <&> (+1)
-- Just 3
--
-- >>> [1,2,3] <&> (+1)
-- [2,3,4]
--
-- >>> Right 3 <&> (+1)
-- Right 4
--
(<&>) :: Functor f => f a -> (a -> b) -> f b
as <&> f = f <$> as

infixl 1 <&>
#endif