ContentsIndex
GenUtil
Contents
Functions
Error reporting
Simple deconstruction
System routines
Random routines
Monad routines
Text Routines
Quoting
Layout
Scrambling
Random
Option handling
Classes
Description
This is a collection of random useful utility functions written in pure Haskell 98. In general, it trys to conform to the naming scheme put forth the haskell prelude and fill in the obvious omissions, as well as provide useful routines in general. To ensure maximum portability, no instances are exported so it may be added to any project without conflicts.
Synopsis
putErr :: String -> IO ()
putErrLn :: String -> IO ()
putErrDie :: String -> IO a
fromLeft :: Either a b -> a
fromRight :: Either a b -> b
fsts :: [(a, b)] -> [a]
snds :: [(a, b)] -> [b]
splitEither :: [Either a b] -> ([a], [b])
rights :: [Either a b] -> [b]
lefts :: [Either a b] -> [a]
exitSuccess :: IO a
exitFailure
epoch :: ClockTime
lookupEnv :: Monad m => String -> IO (m String)
endOfTime :: ClockTime
repMaybe :: (a -> Maybe a) -> a -> a
liftT2 :: (a -> b, c -> d) -> (a, c) -> (b, d)
snub :: Ord a => [a] -> [a]
snubFst :: Ord a => [(a, b)] -> [(a, b)]
snubUnder :: Ord b => (a -> b) -> [a] -> [a]
smerge :: Ord a => [a] -> [a] -> [a]
sortFst :: Ord a => [(a, b)] -> [(a, b)]
groupFst :: Eq a => [(a, b)] -> [[(a, b)]]
foldl' :: (a -> b -> a) -> a -> [b] -> a
fmapLeft :: Functor f => (a -> c) -> f (Either a b) -> f (Either c b)
fmapRight :: Functor f => (b -> c) -> f (Either a b) -> f (Either a c)
isDisjoint :: Eq a => [a] -> [a] -> Bool
isConjoint :: Eq a => [a] -> [a] -> Bool
groupUnder :: Eq b => (a -> b) -> [a] -> [[a]]
sortUnder :: Ord b => (a -> b) -> [a] -> [a]
minimumUnder :: Ord b => (a -> b) -> [a] -> a
maximumUnder :: Ord b => (a -> b) -> [a] -> a
sortGroupUnder :: Ord a => (b -> a) -> [b] -> [[b]]
sortGroupUnderF :: Ord a => (b -> a) -> [b] -> [(a, [b])]
sortGroupUnderFG :: Ord b => (a -> b) -> (a -> c) -> [a] -> [(b, [c])]
naturals :: [Int]
perhapsM :: Monad m => Bool -> a -> m a
repeatM :: Monad m => m a -> m [a]
repeatM_ :: Monad m => m a -> m ()
replicateM :: Monad m => Int -> m a -> m [a]
replicateM_ :: Monad m => Int -> m a -> m ()
maybeToMonad :: Monad m => Maybe a -> m a
toMonadM :: Monad m => m (Maybe a) -> m a
ioM :: Monad m => IO a -> IO (m a)
ioMp :: MonadPlus m => IO a -> IO (m a)
foldlM :: Monad m => (a -> b -> m a) -> a -> [b] -> m a
foldlM_ :: Monad m => (a -> b -> m a) -> a -> [b] -> m ()
foldl1M :: Monad m => (a -> a -> m a) -> [a] -> m a
foldl1M_ :: Monad m => (a -> a -> m a) -> [a] -> m ()
maybeM :: Monad m => String -> Maybe a -> m a
shellQuote :: [String] -> String
simpleQuote :: [String] -> String
simpleUnquote :: String -> [String]
indentLines :: Int -> String -> String
buildTableLL :: [(String, String)] -> [String]
buildTableRL :: [(String, String)] -> [String]
buildTable :: [String] -> [(String, [String])] -> String
trimBlankLines :: String -> String
paragraph :: Int -> String -> String
paragraphBreak :: Int -> String -> String
expandTabs :: String -> String
chunkText :: Int -> String -> String
rot13 :: String -> String
intercalate :: [a] -> [[a]] -> [a]
powerSet :: [a] -> [[a]]
randomPermute :: StdGen -> [a] -> [a]
randomPermuteIO :: [a] -> IO [a]
chunk :: Int -> [a] -> [[a]]
fromEither :: Either a a -> a
mapFst :: (a -> b) -> (a, c) -> (b, c)
mapSnd :: (a -> b) -> (c, a) -> (c, b)
mapFsts :: (a -> b) -> [(a, c)] -> [(b, c)]
mapSnds :: (a -> b) -> [(c, a)] -> [(c, b)]
tr :: String -> String -> String -> String
readHex :: Monad m => String -> m Int
overlaps :: Ord a => (a, a) -> (a, a) -> Bool
showDuration :: Integral a => a -> String
readM :: (Monad m, Read a) => String -> m a
readsM :: (Monad m, Read a) => String -> m (a, String)
split :: (a -> Bool) -> [a] -> [[a]]
tokens :: (a -> Bool) -> [a] -> [[a]]
count :: (a -> Bool) -> [a] -> Int
getArgContents :: IO String
parseOpt :: Monad m => String -> [String] -> m ([String], [Char], [(Char, String)])
getOptContents :: String -> IO (String, [Char], [(Char, String)])
doTime :: String -> IO a -> IO a
getPrefix :: Monad m => String -> String -> m String
rspan :: (a -> Bool) -> [a] -> ([a], [a])
rbreak :: (a -> Bool) -> [a] -> ([a], [a])
rdropWhile :: (a -> Bool) -> [a] -> [a]
rtakeWhile :: (a -> Bool) -> [a] -> [a]
rbdropWhile :: (a -> Bool) -> [a] -> [a]
concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]
on :: (a -> a -> b) -> (c -> a) -> c -> c -> b
mapMsnd :: Monad m => (b -> m c) -> [(a, b)] -> m [(a, c)]
mapMfst :: Monad m => (b -> m c) -> [(b, a)] -> m [(c, a)]
class Monad m => UniqueProducer m where
newUniq :: m Int
Functions
Error reporting
putErr :: String -> IO ()
Flushes stdout and writes string to standard error
putErrLn :: String -> IO ()
Flush stdout and write string and newline to standard error
putErrDie :: String -> IO a
Flush stdout, write string and newline to standard error, then exit program with failure.
Simple deconstruction
fromLeft :: Either a b -> a
fromRight :: Either a b -> b
fsts :: [(a, b)] -> [a]
take the fst of every element of a list
snds :: [(a, b)] -> [b]
take the snd of every element of a list
splitEither :: [Either a b] -> ([a], [b])
partition a list of eithers.
rights :: [Either a b] -> [b]
take just the rights
lefts :: [Either a b] -> [a]
take just the lefts
System routines
exitSuccess :: IO a
exit program successfully. exitFailure is also exported from System.
exitFailure
epoch :: ClockTime
the standard unix epoch
lookupEnv :: Monad m => String -> IO (m String)
looks up an enviornment variable and returns it in an arbitrary Monad rather than raising an exception if the variable is not set.
endOfTime :: ClockTime
an arbitrary time in the future
Random routines
repMaybe :: (a -> Maybe a) -> a -> a
recursivly apply function to value until it returns Nothing
liftT2 :: (a -> b, c -> d) -> (a, c) -> (b, d)
apply functions to values inside a tupele. liftT3 and liftT4 also exist.
snub :: Ord a => [a] -> [a]
sorted nub of list, much more efficient than nub, but doesnt preserve ordering.
snubFst :: Ord a => [(a, b)] -> [(a, b)]
sorted nub of list of tuples, based solely on the first element of each tuple.
snubUnder :: Ord b => (a -> b) -> [a] -> [a]
sorted nub of list based on function of values
smerge :: Ord a => [a] -> [a] -> [a]
merge sorted lists in linear time
sortFst :: Ord a => [(a, b)] -> [(a, b)]
sort list of tuples, based on first element of each tuple.
groupFst :: Eq a => [(a, b)] -> [[(a, b)]]
group list of tuples, based only on equality of the first element of each tuple.
foldl' :: (a -> b -> a) -> a -> [b] -> a
strict version of foldl
fmapLeft :: Functor f => (a -> c) -> f (Either a b) -> f (Either c b)
fmapRight :: Functor f => (b -> c) -> f (Either a b) -> f (Either a c)
isDisjoint :: Eq a => [a] -> [a] -> Bool
set operations on lists. (slow!)
isConjoint :: Eq a => [a] -> [a] -> Bool
groupUnder :: Eq b => (a -> b) -> [a] -> [[a]]
group a list based on a function of the values.
sortUnder :: Ord b => (a -> b) -> [a] -> [a]
sort a list based on a function of the values.
minimumUnder :: Ord b => (a -> b) -> [a] -> a
maximumUnder :: Ord b => (a -> b) -> [a] -> a
sortGroupUnder :: Ord a => (b -> a) -> [b] -> [[b]]
sortGroupUnderF :: Ord a => (b -> a) -> [b] -> [(a, [b])]
sortGroupUnderFG :: Ord b => (a -> b) -> (a -> c) -> [a] -> [(b, [c])]
naturals :: [Int]
Monad routines
perhapsM :: Monad m => Bool -> a -> m a
repeatM :: Monad m => m a -> m [a]
repeatM_ :: Monad m => m a -> m ()
replicateM :: Monad m => Int -> m a -> m [a]
replicateM_ :: Monad m => Int -> m a -> m ()
maybeToMonad :: Monad m => Maybe a -> m a
convert a maybe to an arbitrary failable monad
toMonadM :: Monad m => m (Maybe a) -> m a
ioM :: Monad m => IO a -> IO (m a)
Trasform IO errors into the failing of an arbitrary monad.
ioMp :: MonadPlus m => IO a -> IO (m a)
Trasform IO errors into the mzero of an arbitrary member of MonadPlus.
foldlM :: Monad m => (a -> b -> m a) -> a -> [b] -> m a
foldlM_ :: Monad m => (a -> b -> m a) -> a -> [b] -> m ()
foldl1M :: Monad m => (a -> a -> m a) -> [a] -> m a
foldl1M_ :: Monad m => (a -> a -> m a) -> [a] -> m ()
maybeM :: Monad m => String -> Maybe a -> m a
convert a maybe to an arbitrary failable monad
Text Routines
Quoting
shellQuote :: [String] -> String
quote a set of strings as would be appropriate to pass them as arguments to a sh style shell
simpleQuote :: [String] -> String
quote strings rc style. single quotes protect any characters between them, to get an actual single quote double it up. Inverse of simpleUnquote
simpleUnquote :: String -> [String]
inverse of simpleQuote
Layout
indentLines :: Int -> String -> String
place spaces before each line in string.
buildTableLL :: [(String, String)] -> [String]
buildTableRL :: [(String, String)] -> [String]
buildTable :: [String] -> [(String, [String])] -> String
trimBlankLines :: String -> String
trim blank lines at beginning and end of string
paragraph :: Int -> String -> String
reformat a string to not be wider than a given width, breaking it up between words.
paragraphBreak :: Int -> String -> String
expandTabs :: String -> String
expand tabs into spaces in a string assuming tabs are every 8 spaces and we are starting at column 0.
chunkText :: Int -> String -> String
Scrambling
rot13 :: String -> String
Random
intercalate :: [a] -> [[a]] -> [a]
concat composed with intersperse. Can be used similarly to join in perl.
powerSet :: [a] -> [[a]]
compute the power set of a list
randomPermute :: StdGen -> [a] -> [a]
randomly permute a list given a RNG
randomPermuteIO :: [a] -> IO [a]
randomly permute a list, using the standard random number generator.
chunk :: Int -> [a] -> [[a]]
fromEither :: Either a a -> a
mapFst :: (a -> b) -> (a, c) -> (b, c)
mapSnd :: (a -> b) -> (c, a) -> (c, b)
mapFsts :: (a -> b) -> [(a, c)] -> [(b, c)]
mapSnds :: (a -> b) -> [(c, a)] -> [(c, b)]
tr :: String -> String -> String -> String
Translate characters to other characters in a string, if the second argument is empty, delete the characters in the first argument, else map each character to the cooresponding one in the second argument, cycling the second argument if necessary.
readHex :: Monad m => String -> m Int
overlaps :: Ord a => (a, a) -> (a, a) -> Bool
determine if two closed intervals overlap at all.
showDuration :: Integral a => a -> String
translate a number of seconds to a string representing the duration expressed.
readM :: (Monad m, Read a) => String -> m a
readsM :: (Monad m, Read a) => String -> m (a, String)
split :: (a -> Bool) -> [a] -> [[a]]

Splits a list into components delimited by separators, where the predicate returns True for a separator element. The resulting components do not contain the separators. Two adjacent separators result in an empty component in the output. eg.

 split (=='a') "aabbaca"
 ["", "", "bb", "c", ""]
tokens :: (a -> Bool) -> [a] -> [[a]]

Like split, except that sequences of adjacent separators are treated as a single separator. eg.

 tokens (=='a') "aabbaca"
 ["bb","c"]
count :: (a -> Bool) -> [a] -> Int
count elements of list that have a given property
Option handling
getArgContents :: IO String
behave like while() in perl, go through the argument list, reading the concation of each file name mentioned or stdin if '-' is on it. If no arguments are given, read stdin.
parseOpt
:: Monad m
=> StringArgument string, list of valid options with : after ones which accept an argument
-> [String]Arguments
-> m ([String], [Char], [(Char, String)])(non-options,flags,options with arguments)
Process options with an option string like the standard C getopt function call.
getOptContents :: String -> IO (String, [Char], [(Char, String)])
Combination of parseOpt and getArgContents.
doTime :: String -> IO a -> IO a
time task
getPrefix :: Monad m => String -> String -> m String
rspan :: (a -> Bool) -> [a] -> ([a], [a])
rbreak :: (a -> Bool) -> [a] -> ([a], [a])
rdropWhile :: (a -> Bool) -> [a] -> [a]
rtakeWhile :: (a -> Bool) -> [a] -> [a]
rbdropWhile :: (a -> Bool) -> [a] -> [a]
concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]
on :: (a -> a -> b) -> (c -> a) -> c -> c -> b
mapMsnd :: Monad m => (b -> m c) -> [(a, b)] -> m [(a, c)]
mapMfst :: Monad m => (b -> m c) -> [(b, a)] -> m [(c, a)]
Classes
class Monad m => UniqueProducer m where
class for monads which can generate unique values.
Methods
newUniq :: m Int
produce a new unique value
Produced by Haddock version 0.8