Description : utility functions that can't be found in the libraries
Copyright : (c) Klaus L�ttich, Uni Bremen 2002-2006
Maintainer : luettich@tzi.de
Utility functions that can't be found in the libraries
but should be shared across Hets.
-- | keep only minimal elements
keepMins :: (a -> a -> Bool) -> [a] -> [a]
keepMins lt l = case l of
x : r -> let s = filter ( \ y -> not (lt x y)) r
in if any ( \ y -> lt y x) s then m
A function inspired by perls join function. It joins a list of
lists of elements by seperating them with a seperator element.
joinWith :: a -- ^ seperator element
-> [[a]] -- ^ list of lists of elements
joinWith sep = concat . intersperse [sep]
A function inspired by the perl function split. A list is splitted
on a seperator element in smaller non-empty lists.
The seperator element is dropped from the resulting list.
splitOn :: Eq a => a -- ^ seperator
-> [a] -- ^ list to split
splitOn x xs = let (l, r) = break (==x) xs in
(if null l then [] else [l]) ++ (if null r then [] else splitOn x $ tail r)
A function inspired by a perl function from the standard perl-module
File::Basename. It removes the directory part of a filepath.
basename :: FilePath -> FilePath
basename fp = (\(_path,basen) -> basen) (stripDir fp)
A function inspired by a perl function from the standard perl-module
File::Basename. It gives the directory part of a filepath.
dirname :: FilePath -> FilePath
dirname fp = (\(path,_basen) -> path) (stripDir fp)
A function inspired by a perl function from the standard perl-module
File::Basename. It splits a filepath into the basename, the
directory and gives the suffix that matched from the list of
suffixes. If a suffix matched it is removed from the basename.
fileparse :: [String] -- ^ list of suffixes
-> (FilePath,FilePath,Maybe String)
-- ^ (basename,directory,matched suffix)
fileparse sufs fp = let (path,base) = stripDir fp
(base',suf) = stripSuffix sufs base
stripDir :: FilePath -> (FilePath,FilePath)
(\(x,y) -> (if not (null y) then reverse y else "./", reverse x))
(break (== '/') (reverse fp))
stripSuffix :: [String] -> FilePath -> (FilePath,Maybe String)
stripSuffix suf fp = case filter justs $ map (stripSuf fp) suf of
((Just (x,s)):_) -> (x,Just s)
where stripSuf f s | s `isSuffixOf` f =
Just (take (length f - length s) f, s)
{- | filter a map according to a given list of keys (it dosen't hurt
if a key is not present in the map) -}
filterMapWithList l = filterMapWithSet sl
{- | filter a map according to a given set of keys (it dosen't hurt if
a key is not present in the map) -}
{- | get, parse and check an environment variable; provide the default
value, only if the envionment variable is not set or the
parse-check-function returns a Left value -}
getEnvSave :: a -- ^ default value
-> String -- ^ name of environment variable
-> (String -> Either b a) -- ^ parse and check value of variable;
-- for every b the default value is returned
getEnvSave defValue envVar readFun = do
if isDoesNotExistError ie -- == NoSuchThing
then return $ Left defValue
return (either id (\s -> (either (const defValue) id (readFun s))) is)