extra-0.2: Extra functions I use.

Safe HaskellNone

Extra

Contents

Synopsis

Control.Concurrent.Extra

withNumCapabilities :: Int -> IO a -> IO aSource

On GHC 7.6 and above with the -threaded flag, brackets a call to setNumCapabilities. On lower versions (which lack setNumCapabilities) this function just runs the argument action.

setNumCapabilities :: Int -> IO ()Source

A version of setNumCapabilities that works on all versions of GHC, but has no effect before GHC 7.6.

Control.Exception.Extra

retry :: Int -> IO a -> IO aSource

showException :: SomeException -> IO StringSource

Print an exception, but if that exception itself contains exceptions, simply print <NestedException>. Since Haskell is a lazy language it is possible to throw exceptions that are themselves undefined. This function is useful to report them to users.

ignore :: IO () -> IO ()Source

Ignore any exceptions thrown by the action and continue as normal.

catch_ :: IO a -> (SomeException -> IO a) -> IO aSource

handle_ :: (SomeException -> IO a) -> IO a -> IO aSource

catchJust_ :: (SomeException -> Maybe b) -> IO a -> (b -> IO a) -> IO aSource

handleJust_ :: (SomeException -> Maybe b) -> (b -> IO a) -> IO a -> IO aSource

tryJust_ :: (SomeException -> Maybe b) -> IO a -> IO (Either b a)Source

catchBool :: Exception e => (e -> Bool) -> IO a -> (e -> IO a) -> IO aSource

handleBool :: Exception e => (e -> Bool) -> (e -> IO a) -> IO a -> IO aSource

tryBool :: Exception e => (e -> Bool) -> IO a -> IO (Either e a)Source

Control.Monad.Extra

whenJust :: Applicative m => Maybe a -> (a -> m ()) -> m ()Source

unit :: m () -> m ()Source

partitionM :: Monad m => (a -> m Bool) -> [a] -> m ([a], [a])Source

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

loopM :: Monad m => (a -> m (Either a b)) -> a -> m bSource

whileM :: Monad m => m Bool -> m ()Source

ifM :: Monad m => m Bool -> m a -> m a -> m aSource

notM :: Functor m => m Bool -> m BoolSource

(||^) :: Monad m => m Bool -> m Bool -> m BoolSource

(&&^) :: Monad m => m Bool -> m Bool -> m BoolSource

orM :: Monad m => [m Bool] -> m BoolSource

andM :: Monad m => [m Bool] -> m BoolSource

anyM :: Monad m => (a -> m Bool) -> [a] -> m BoolSource

allM :: Monad m => (a -> m Bool) -> [a] -> m BoolSource

findM :: Monad m => (a -> m Bool) -> [a] -> m (Maybe a)Source

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

Data.Either.Extra

fromLeft :: Either t t1 -> tSource

fromRight :: Either t t1 -> t1Source

Data.IORef.Extra

modifyIORef' :: IORef a -> (a -> a) -> IO ()

Strict version of modifyIORef

writeIORef' :: IORef a -> a -> IO ()Source

atomicModifyIORef' :: IORef a -> (a -> (a, b)) -> IO b

Strict version of atomicModifyIORef. This forces both the value stored in the IORef as well as the value returned.

atomicWriteIORef :: IORef a -> a -> IO ()

Variant of writeIORef with the "barrier to reordering" property that atomicModifyIORef has.

Data.List.Extra

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

drop1 :: [a] -> [a]Source

list :: b -> (a -> [a] -> b) -> [a] -> bSource

uncons :: [a] -> Maybe (a, [a])Source

unsnoc :: [a] -> Maybe ([a], a)Source

cons :: a -> [a] -> [a]Source

snoc :: [a] -> a -> [a]Source

groupSort :: Ord k => [(k, v)] -> [(k, [v])]Source

groupSortOn :: Ord a => (k -> a) -> [(k, v)] -> [(k, [v])]Source

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

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

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

chop :: ([a] -> (b, [a])) -> [a] -> [b]Source

for :: [a] -> (a -> b) -> [b]Source

rep :: Eq a => a -> a -> a -> aSource

reps :: Eq a => a -> a -> [a] -> [a]Source

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

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

dropEnd :: Int -> [a] -> [a]Source

takeEnd :: Int -> [a] -> [a]Source

breakEnd :: (a -> Bool) -> [a] -> ([a], [a])Source

spanEnd :: (a -> Bool) -> [a] -> ([a], [a])Source

dropWhileEnd :: (a -> Bool) -> [a] -> [a]

The dropWhileEnd function drops the largest suffix of a list in which the given predicate holds for all elements. For example:

 dropWhileEnd isSpace "foo\n" == "foo"
 dropWhileEnd isSpace "foo bar" == "foo bar"
 dropWhileEnd isSpace ("foo\n" ++ undefined) == "foo" ++ undefined

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

stripSuffix :: Eq a => [a] -> [a] -> Maybe [a]Source

Return the prefix of the second string if its suffix matches the entire first string.

Examples:

 stripSuffix "bar" "foobar" == Just "foo"
 stripSuffix ""    "baz"    == Just "baz"
 stripSuffix "foo" "quux"   == Nothing

concatUnzip :: [([a], [b])] -> ([a], [b])Source

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

mergeBy :: (a -> a -> Ordering) -> [a] -> [a] -> [a]Source

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

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

firstJust :: (a -> Maybe b) -> [a] -> Maybe bSource

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

Find the first instance of needle in haystack. The first element of the returned tuple is the prefix of haystack before needle is matched. The second is the remainder of haystack, starting with the match.

Examples:

 breakOn "::" "a::b::c" == ("a", "::b::c")
 breakOn "/" "foobar"   == ("foobar", "")

Laws:

 \needle haystack -> let (prefix,match) = breakOn needle haystack in prefix ++ match == haystack

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

Similar to breakOn, but searches from the end of the string.

The first element of the returned tuple is the prefix of haystack up to and including the last match of needle. The second is the remainder of haystack, following the match.

 breakOnEnd "::" "a::b::c" == ("a::b::", "c")

splitOn :: Eq a => [a] -> [a] -> [[a]]Source

Break a list into pieces separated by the first list argument, consuming the delimiter. An empty delimiter is invalid, and will cause an error to be raised.

Examples:

 splitOn "\r\n" "a\r\nb\r\nd\r\ne" == ["a","b","d","e"]
 splitOn "aaa"  "aaaXaaaXaaaXaaa"  == ["","X","X","X",""]
 splitOn "x"    "x"                == ["",""]
 splitOn "x"    ""                 == [""]

and

 \s x -> s /= "" ==> intercalate s (splitOn s x) == x
 \c x -> splitOn [c] x                           == split (==c) x

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",""]
 split (=='a') ""        == [""]

chunksOf :: Int -> [a] -> [[a]]Source

Data.Tuple.Extra

dupe :: a -> (a, a)Source

fst3 :: (a, b, c) -> aSource

snd3 :: (a, b, c) -> bSource

thd3 :: (a, b, c) -> cSource

concat2 :: [([a], [b])] -> ([a], [b])Source

concat3 :: [([a], [b], [c])] -> ([a], [b], [c])Source

Numeric.Extra

System.Directory.Extra

System.Environment.Extra

getExecutablePath :: IO FilePath

Returns the absolute pathname of the current executable.

Note that for scripts and interactive sessions, this is the path to the interpreter (e.g. ghci.)

lookupEnv :: String -> IO (Maybe String)

Return the value of the environment variable var, or Nothing if there is no such value.

For POSIX users, this is equivalent to getEnv.

System.Info.Extra

System.IO.Extra

System.Time.Extra

offsetTime :: IO (IO Double)Source

Call once at the start, then call repeatedly to get Time values out

offsetTimeIncrease :: IO (IO Double)Source

Like offsetTime, but results will never decrease (though they may stay the same)

duration :: IO a -> IO (Double, a)Source