Safe Haskell | None |
---|

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.

- 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]
- isLeft :: Either t t1 -> Bool
- isRight :: Either t t1 -> Bool
- fst3 :: (t, t1, t2) -> t
- snd3 :: (t, t1, t2) -> t1
- thd3 :: (t, t1, t2) -> t2
- exitSuccess :: IO a
- exitFailure :: IO a
- 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)
- liftT3 :: (t -> t3, t1 -> t4, t2 -> t5) -> (t, t1, t2) -> (t3, t4, t5)
- liftT4 :: (t -> t4, t1 -> t5, t2 -> t6, t3 -> t7) -> (t, t1, t2, t3) -> (t4, t5, t6, t7)
- 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])]
- sameLength :: [t] -> [t1] -> Bool
- 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]]
- rtup :: t1 -> t -> (t, t1)
- triple :: t -> t1 -> t2 -> (t, t1, t2)
- 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 :: (Show a, 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
- hasRepeatUnder :: Ord a1 => (a -> a1) -> [a] -> Bool
- 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)]
- iocatch :: IO a -> (IOException -> IO a) -> IO a
- class Monad m => UniqueProducer m where

# Functions

## Error reporting

putErrDie :: String -> IO aSource

Flush stdout, write string and newline to standard error, then exit program with failure.

## Simple deconstruction

splitEither :: [Either a b] -> ([a], [b])Source

partition a list of eithers.

## System routines

exitSuccess :: IO a

The computation `exitSuccess`

is equivalent to
`exitWith`

`ExitSuccess`

, It terminates the program
successfully.

exitFailure :: IO a

The computation `exitFailure`

is equivalent to
`exitWith`

`(`

`ExitFailure`

*exitfail*`)`

,
where *exitfail* is implementation-dependent.

lookupEnv :: Monad m => String -> IO (m String)Source

looks up an enviornment variable and returns it in an arbitrary Monad rather than raising an exception if the variable is not set.

## Random routines

repMaybe :: (a -> Maybe a) -> a -> aSource

recursivly apply function to value until it returns Nothing

snub :: Ord a => [a] -> [a]Source

sorted nub of list, much more efficient than nub, but doesnt preserve ordering.

snubFst :: Ord a => [(a, b)] -> [(a, b)]Source

sorted nub of list of tuples, based solely on the first element of each tuple.

sortFst :: Ord a => [(a, b)] -> [(a, b)]Source

sort list of tuples, based on first element of each tuple.

groupFst :: Eq a => [(a, b)] -> [[(a, b)]]Source

group list of tuples, based only on equality of the first element of each tuple.

isDisjoint :: Eq a => [a] -> [a] -> BoolSource

set operations on lists. (slow!)

isConjoint :: Eq a => [a] -> [a] -> BoolSource

set operations on lists. (slow!)

groupUnder :: Eq b => (a -> b) -> [a] -> [[a]]Source

group a list based on a function of the values.

minimumUnder :: Ord b => (a -> b) -> [a] -> aSource

maximumUnder :: Ord b => (a -> b) -> [a] -> aSource

sortGroupUnder :: Ord a => (b -> a) -> [b] -> [[b]]Source

sortGroupUnderF :: Ord a => (b -> a) -> [b] -> [(a, [b])]Source

sortGroupUnderFG :: Ord b => (a -> b) -> (a -> c) -> [a] -> [(b, [c])]Source

sameLength :: [t] -> [t1] -> BoolSource

## Monad routines

replicateM :: Monad m => Int -> m a -> m [a]Source

replicateM_ :: Monad m => Int -> m a -> m ()Source

maybeToMonad :: Monad m => Maybe a -> m aSource

convert a maybe to an arbitrary failable monad

ioMp :: MonadPlus m => IO a -> IO (m a)Source

Trasform IO errors into the mzero of an arbitrary member of MonadPlus.

## Text Routines

### Quoting

shellQuote :: [String] -> StringSource

quote a set of strings as would be appropriate to pass them as arguments to a sh style shell

simpleQuote :: [String] -> StringSource

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]Source

inverse of `simpleQuote`

### Layout

indentLines :: Int -> String -> StringSource

place spaces before each line in string.

buildTableLL :: [(String, String)] -> [String]Source

buildTableRL :: [(String, String)] -> [String]Source

trimBlankLines :: String -> StringSource

trim blank lines at beginning and end of string

paragraph :: Int -> String -> StringSource

reformat a string to not be wider than a given width, breaking it up between words.

paragraphBreak :: Int -> String -> StringSource

expandTabs :: String -> StringSource

expand tabs into spaces in a string assuming tabs are every 8 spaces and we are starting at column 0.

### Scrambling

## Random

intercalate :: [a] -> [[a]] -> [a]

`intercalate`

`xs xss`

is equivalent to `(`

.
It inserts the list `concat`

(`intersperse`

xs xss))`xs`

in between the lists in `xss`

and concatenates the
result.

randomPermute :: StdGen -> [a] -> [a]Source

randomly permute a list given a RNG

randomPermuteIO :: [a] -> IO [a]Source

randomly permute a list, using the standard random number generator.

fromEither :: Either a a -> aSource

tr :: String -> String -> String -> StringSource

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.

overlaps :: Ord a => (a, a) -> (a, a) -> BoolSource

determine if two closed intervals overlap at all.

showDuration :: (Show a, Integral a) => a -> StringSource

translate a number of seconds to a string representing the duration expressed.

split :: (a -> Bool) -> [a] -> [[a]]Source

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]]Source

Like `split`

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

tokens (=='a') "aabbaca" ["bb","c"]

hasRepeatUnder :: Ord a1 => (a -> a1) -> [a] -> BoolSource

## Option handling

getArgContents :: IO StringSource

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.

:: Monad m | |

=> String | Argument 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)])Source

Combination of parseOpt and getArgContents.

rdropWhile :: (a -> Bool) -> [a] -> [a]Source

rtakeWhile :: (a -> Bool) -> [a] -> [a]Source

rbdropWhile :: (a -> Bool) -> [a] -> [a]Source

concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]Source

# Classes

class Monad m => UniqueProducer m whereSource

class for monads which can generate unique values.

UniqueProducer IO | |

UniqueProducer Tc | |

(Monad m, Monad (t m), MonadTrans t, UniqueProducer m) => UniqueProducer (t m) | |

Monad m => UniqueProducer (UniqT m) |