diff options
Diffstat (limited to 'Utility')
-rw-r--r-- | Utility/Format.hs | 4 | ||||
-rw-r--r-- | Utility/Monad.hs | 10 | ||||
-rw-r--r-- | Utility/PartialPrelude.hs | 2 | ||||
-rw-r--r-- | Utility/TempFile.hs | 4 |
4 files changed, 12 insertions, 8 deletions
diff --git a/Utility/Format.hs b/Utility/Format.hs index 2c2042cc2..d8b7e4549 100644 --- a/Utility/Format.hs +++ b/Utility/Format.hs @@ -37,9 +37,11 @@ data Frag = Const String | Var String Justify data Justify = LeftJustified Int | RightJustified Int | UnJustified deriving (Show) +type Variables = M.Map String String + {- Expands a Format using some variables, generating a formatted string. - This can be repeatedly called, efficiently. -} -format :: Format -> M.Map String String -> String +format :: Format -> Variables -> String format f vars = concatMap expand f where expand (Const s) = s diff --git a/Utility/Monad.hs b/Utility/Monad.hs index 95964361e..28aa33ee8 100644 --- a/Utility/Monad.hs +++ b/Utility/Monad.hs @@ -12,7 +12,7 @@ import Control.Monad (liftM) {- Return the first value from a list, if any, satisfying the given - predicate -} -firstM :: (Monad m) => (a -> m Bool) -> [a] -> m (Maybe a) +firstM :: Monad m => (a -> m Bool) -> [a] -> m (Maybe a) firstM _ [] = return Nothing firstM p (x:xs) = do q <- p x @@ -22,20 +22,20 @@ firstM p (x:xs) = do {- Returns true if any value in the list satisfies the predicate, - stopping once one is found. -} -anyM :: (Monad m) => (a -> m Bool) -> [a] -> m Bool +anyM :: Monad m => (a -> m Bool) -> [a] -> m Bool anyM p = liftM isJust . firstM p {- Runs an action on values from a list until it succeeds. -} -untilTrue :: (Monad m) => [a] -> (a -> m Bool) -> m Bool +untilTrue :: Monad m => [a] -> (a -> m Bool) -> m Bool untilTrue = flip anyM {- Runs an action, passing its value to an observer before returning it. -} -observe :: (Monad m) => (a -> m b) -> m a -> m a +observe :: Monad m => (a -> m b) -> m a -> m a observe observer a = do r <- a _ <- observer r return r {- b `after` a runs first a, then b, and returns the value of a -} -after :: (Monad m) => m b -> m a -> m a +after :: Monad m => m b -> m a -> m a after = observe . const diff --git a/Utility/PartialPrelude.hs b/Utility/PartialPrelude.hs index ad857196d..507fc6252 100644 --- a/Utility/PartialPrelude.hs +++ b/Utility/PartialPrelude.hs @@ -37,7 +37,7 @@ last = Prelude.last - Ignores leading/trailing whitespace, and throws away any trailing - text after the part that can be read. -} -readMaybe :: (Read a) => String -> Maybe a +readMaybe :: Read a => String -> Maybe a readMaybe s = case reads s of ((x,_):_) -> Just x _ -> Nothing diff --git a/Utility/TempFile.hs b/Utility/TempFile.hs index 3887b422b..469d52e8c 100644 --- a/Utility/TempFile.hs +++ b/Utility/TempFile.hs @@ -26,8 +26,10 @@ viaTmp a file content = do a tmpfile content renameFile tmpfile file +type Template = String + {- Runs an action with a temp file, then removes the file. -} -withTempFile :: String -> (FilePath -> Handle -> IO a) -> IO a +withTempFile :: Template -> (FilePath -> Handle -> IO a) -> IO a withTempFile template a = bracket create remove use where create = do |