treak
This commit is contained in:
parent
eb9001044f
commit
183bdacca2
4 changed files with 12 additions and 8 deletions
|
@ -37,9 +37,11 @@ data Frag = Const String | Var String Justify
|
||||||
data Justify = LeftJustified Int | RightJustified Int | UnJustified
|
data Justify = LeftJustified Int | RightJustified Int | UnJustified
|
||||||
deriving (Show)
|
deriving (Show)
|
||||||
|
|
||||||
|
type Variables = M.Map String String
|
||||||
|
|
||||||
{- Expands a Format using some variables, generating a formatted string.
|
{- Expands a Format using some variables, generating a formatted string.
|
||||||
- This can be repeatedly called, efficiently. -}
|
- This can be repeatedly called, efficiently. -}
|
||||||
format :: Format -> M.Map String String -> String
|
format :: Format -> Variables -> String
|
||||||
format f vars = concatMap expand f
|
format f vars = concatMap expand f
|
||||||
where
|
where
|
||||||
expand (Const s) = s
|
expand (Const s) = s
|
||||||
|
|
|
@ -12,7 +12,7 @@ import Control.Monad (liftM)
|
||||||
|
|
||||||
{- Return the first value from a list, if any, satisfying the given
|
{- Return the first value from a list, if any, satisfying the given
|
||||||
- predicate -}
|
- 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 _ [] = return Nothing
|
||||||
firstM p (x:xs) = do
|
firstM p (x:xs) = do
|
||||||
q <- p x
|
q <- p x
|
||||||
|
@ -22,20 +22,20 @@ firstM p (x:xs) = do
|
||||||
|
|
||||||
{- Returns true if any value in the list satisfies the predicate,
|
{- Returns true if any value in the list satisfies the predicate,
|
||||||
- stopping once one is found. -}
|
- 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
|
anyM p = liftM isJust . firstM p
|
||||||
|
|
||||||
{- Runs an action on values from a list until it succeeds. -}
|
{- 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
|
untilTrue = flip anyM
|
||||||
|
|
||||||
{- Runs an action, passing its value to an observer before returning it. -}
|
{- 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
|
observe observer a = do
|
||||||
r <- a
|
r <- a
|
||||||
_ <- observer r
|
_ <- observer r
|
||||||
return r
|
return r
|
||||||
|
|
||||||
{- b `after` a runs first a, then b, and returns the value of a -}
|
{- 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
|
after = observe . const
|
||||||
|
|
|
@ -37,7 +37,7 @@ last = Prelude.last
|
||||||
- Ignores leading/trailing whitespace, and throws away any trailing
|
- Ignores leading/trailing whitespace, and throws away any trailing
|
||||||
- text after the part that can be read.
|
- 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
|
readMaybe s = case reads s of
|
||||||
((x,_):_) -> Just x
|
((x,_):_) -> Just x
|
||||||
_ -> Nothing
|
_ -> Nothing
|
||||||
|
|
|
@ -26,8 +26,10 @@ viaTmp a file content = do
|
||||||
a tmpfile content
|
a tmpfile content
|
||||||
renameFile tmpfile file
|
renameFile tmpfile file
|
||||||
|
|
||||||
|
type Template = String
|
||||||
|
|
||||||
{- Runs an action with a temp file, then removes the file. -}
|
{- 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
|
withTempFile template a = bracket create remove use
|
||||||
where
|
where
|
||||||
create = do
|
create = do
|
||||||
|
|
Loading…
Reference in a new issue