Safe Haskell | Safe-Infered |
---|
Utility functions.
- debug :: Show a => a -> a
- debugFn :: Show b => (a -> b) -> a -> a
- debugXy :: Show a => a -> b -> b
- applyIf :: Bool -> (a -> a) -> a -> a
- commaJoin :: [String] -> String
- sepSplit :: Eq a => a -> [a] -> [[a]]
- plural :: Int -> String -> String -> String
- ensureQuoted :: String -> String
- stdDev :: [Double] -> Double
- if' :: Bool -> a -> a -> a
- parseChoices :: (Monad m, Read a) => String -> String -> [(a, String)] -> m a
- tryRead :: (Monad m, Read a) => String -> String -> m a
- formatTable :: [[String]] -> [Bool] -> [[String]]
- printTable :: String -> [String] -> [[String]] -> [Bool] -> String
- parseUnitValue :: Monad m => String -> m Rational
- parseUnit :: (Monad m, Integral a, Read a) => String -> m a
- exitIfBad :: String -> Result a -> IO a
- exitErr :: String -> IO a
- exitWhen :: Bool -> String -> IO ()
- exitUnless :: Bool -> String -> IO ()
- extractKey :: [Either Integer String] -> String -> ([Either Integer String], String)
- niceSort :: [String] -> [String]
- niceSortKey :: (a -> String) -> [a] -> [a]
- rStripSpace :: String -> String
- newUUID :: IO String
- getCurrentTime :: IO Integer
- getCurrentTimeUSec :: IO Integer
- clockTimeToString :: ClockTime -> String
- chompPrefix :: String -> String -> Maybe String
- wrap :: Int -> String -> [String]
- trim :: String -> String
- defaultHead :: a -> [a] -> a
- exitIfEmpty :: String -> [a] -> IO a
Debug functions
debugFn :: Show b => (a -> b) -> a -> aSource
Displays a modified form of the second parameter before returning it.
Miscellaneous
applyIf :: Bool -> (a -> a) -> a -> aSource
Apply the function if condition holds, otherwise use default value.
ensureQuoted :: String -> StringSource
Ensure a value is quoted if needed.
Mathematical functions
Logical functions
:: Bool | condition |
-> a | "then" result |
-> a | "else" result |
-> a | "then" or else result depending on the condition |
"if" as a function, rather than as syntactic sugar.
Parsing utility functions
parseChoices :: (Monad m, Read a) => String -> String -> [(a, String)] -> m aSource
tryRead :: (Monad m, Read a) => String -> String -> m aSource
Safe read
function returning data encapsulated in a Result.
formatTable :: [[String]] -> [Bool] -> [[String]]Source
Format a table of strings to maintain consistent length.
printTable :: String -> [String] -> [[String]] -> [Bool] -> StringSource
Constructs a printable table from given header and rows
parseUnitValue :: Monad m => String -> m RationalSource
parseUnit :: (Monad m, Integral a, Read a) => String -> m aSource
Tries to extract number and scale from the given string.
Input must be in the format NUMBER+ SPACE* [UNIT]. If no unit is specified, it defaults to MiB. Return value is always an integral value in MiB.
exitWhen :: Bool -> String -> IO ()Source
Exits with an error message if the given boolean condition if true.
exitUnless :: Bool -> String -> IO ()Source
Exits with an error message unless the given boolean condition
if true, the opposite of exitWhen
.
extractKey :: [Either Integer String] -> String -> ([Either Integer String], String)Source
niceSort :: [String] -> [String]Source
Sort a list of strings based on digit and non-digit groupings.
Given a list of names [
this function
will sort the list in the logical order a1
, a10
, a11
, a2
][
.
a1
, a2
, a10
, a11
]
The sort algorithm breaks each name in groups of either only-digits or no-digits, and sorts based on each group.
Internally, this is not implemented via regexes (like the Python
version), but via actual splitting of the string in sequences of
either digits or everything else, and converting the digit sequences
in Left Integer and the non-digit ones in Right String, at which
point sorting becomes trivial due to the built-in Either
ordering;
we only need one extra step of dropping the key at the end.
niceSortKey :: (a -> String) -> [a] -> [a]Source
Key-version of niceSort
. We use sortBy
and compare
since we don't want to add an ordering constraint on the a type,
hence the need to only compare the first element of the (key, a)
tuple.
on
fst
rStripSpace :: String -> StringSource
Strip space characthers (including newline). As this is expensive, should only be run on small strings.
Returns a random UUID. This is a Linux-specific method as it uses the /proc filesystem.
getCurrentTime :: IO IntegerSource
Returns the current time as an Integer
representing the number
of seconds from the Unix epoch.
getCurrentTimeUSec :: IO IntegerSource
Returns the current time as an Integer
representing the number
of microseconds from the Unix epoch (hence the need for Integer
).
clockTimeToString :: ClockTime -> StringSource
Convert a ClockTime into a (seconds-only) timestamp.
chompPrefix :: String -> String -> Maybe StringSource
Strip a prefix from a string, allowing the last character of the prefix (which is assumed to be a separator) to be absent from the string if the string terminates there.
>>> chompPrefix "foo:bar:" "a:b:c" Nothing
>>> chompPrefix "foo:bar:" "foo:bar:baz" Just "baz"
>>> chompPrefix "foo:bar:" "foo:bar:" Just ""
>>> chompPrefix "foo:bar:" "foo:bar" Just ""
>>> chompPrefix "foo:bar:" "foo:barbaz" Nothing
:: Int | maxWidth |
-> String | string that needs wrapping |
-> [String] | string "broken" in lines |
Breaks a string in lines with length <= maxWidth.
NOTE: The split is OK if:
- It doesn't break a word, i.e. the next line begins with space
(
isSpace . head $ rest
) or the current line ends with space (null revExtra
); - It breaks a very big word that doesn't fit anyway (
null revLine
).
defaultHead :: a -> [a] -> aSource
A safer head version, with a default value.
exitIfEmpty :: String -> [a] -> IO aSource
A head
version in the I/O monad, for validating parameters
without which we cannot continue.