This commit is contained in:
Joey Hess 2012-01-21 02:24:12 -04:00
parent eb9001044f
commit 183bdacca2
4 changed files with 12 additions and 8 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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