Hets - the Heterogeneous Tool Set

Copyright(c) Klaus Luettich, Uni Bremen 2002-2006
LicenseGPLv2 or higher, see LICENSE.txt
MaintainerChristian.Maeder@dfki.de
Stabilityprovisional
Portabilityportable
Safe HaskellNone

Common.Utils

Description

Utility functions that can't be found in the libraries but should be shared across Hets.

Synopsis

Documentation

isSingleton :: Set a -> Bool

O(1) test if the set's size is one

replace :: Eq a => [a] -> [a] -> [a] -> [a]

replace all occurrences of the first (non-empty sublist) argument with the second argument in the third (list) argument.

hasMany :: Set a -> Bool

O(1) test if the set's size is greater one

number :: [a] -> [(a, Int)]

add indices to a list starting from one

combine :: [[a]] -> [[a]]

Transform a list [l1, l2, ... ln] to (in sloppy notation) [[x1, x2, ... xn] | x1 <- l1, x2 <- l2, ... xn <- ln] (this is just the sequence function!)

trim :: String -> String

trims a string both on left and right hand side

trimLeft :: String -> String

trims a string only on the left side

trimRight :: String -> String

trims a string only on the right side

nubOrd :: Ord a => [a] -> [a]

nubOrdOn :: Ord b => (a -> b) -> [a] -> [a]

atMaybe :: [a] -> Int -> Maybe a

safe variant of !!

readMaybe :: Read a => String -> Maybe a

mapAccumLM

Arguments

:: Monad m 
=> (acc -> x -> m (acc, y))

Function taking accumulator and list element, returning new accumulator and modified list element

-> acc

Initial accumulator

-> [x]

Input list

-> m (acc, [y])

Final accumulator and result list

generalization of mapAccumL to monads

mapAccumLCM :: Monad m => (a -> b -> c) -> (acc -> a -> m (acc, b)) -> acc -> [a] -> m (acc, [c])

generalization of mapAccumL to monads with combine function

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

Monadic version of concatMap taken from http:/darcs.haskell.orgghccompilerutils/MonadUtils.hs

composeMap :: Ord a => Map a b -> Map a a -> Map a a -> Map a a

composition of arbitrary maps

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

keep only minimal elements

splitOn

Arguments

:: Eq a 
=> a

separator

-> [a]

list to split

-> [[a]] 

A function inspired by the perl function split. A list is splitted on a separator element in smaller non-empty lists. The separator element is dropped from the resulting list.

splitPaths :: String -> [FilePath]

split a colon (or on windows semicolon) separated list of paths

splitBy

Arguments

:: Eq a 
=> a

separator

-> [a]

list to split

-> [[a]] 

Same as splitOn but empty lists are kept. Even the empty list is split into a singleton list containing the empty list.

splitByList :: Eq a => [a] -> [a] -> [[a]]

Same as splitBy but the separator is a sublist not only one element. Note that the separator must be non-empty.

numberSuffix :: String -> Maybe (String, Int)

If the given string is terminated by a decimal number this number and the nonnumber prefix is returned.

basename :: FilePath -> FilePath

A function inspired by a perl function from the standard perl-module File::Basename. It removes the directory part of a filepath.

dirname :: FilePath -> FilePath

A function inspired by a perl function from the standard perl-module File::Basename. It gives the directory part of a filepath.

fileparse

Arguments

:: [String]

list of suffixes

-> FilePath 
-> (FilePath, FilePath, Maybe String)

(basename,directory,matched suffix)

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.

makeRelativeDesc

Arguments

:: FilePath

path to a directory

-> FilePath

to be computed relatively to given directory

-> FilePath

resulting relative path

This function generalizes makeRelative in that it computes also a relative path with descents such as ....test.txt

getEnvSave

Arguments

:: a

default value

-> String

name of environment variable

-> (String -> Maybe a)

parse and check value of variable

-> IO a 

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 Nothing

getEnvDef

Arguments

:: String

environment variable

-> String

default value

-> IO String 

get environment variable

filterMapWithList :: Ord k => [k] -> Map k e -> Map k e

filter a map according to a given list of keys (it dosen't hurt if a key is not present in the map)

timeoutSecs :: Int -> IO a -> IO (Maybe a)

the timeout function taking seconds instead of microseconds

executeProcess

Arguments

:: FilePath

command to run

-> [String]

any arguments

-> String

standard input

-> IO (ExitCode, String, String)

exitcode, stdout, stderr

like readProcessWithExitCode, but checks the command argument first

timeoutCommand :: Int -> FilePath -> [String] -> IO (Maybe (ExitCode, String, String))

runs a command with timeout

withinDirectory :: FilePath -> IO a -> IO a

runs an action in a different directory without changing the current directory globally.

writeTempFile

Arguments

:: String

Content

-> FilePath

Directory in which to create the file

-> String

File name template

-> IO FilePath

create file

calls openTempFile but directly writes content and closes the file

getTempFile

Arguments

:: String

Content

-> String

File name template

-> IO FilePath

create file

create file in temporary directory (the first argument is the content)

readFifo :: FilePath -> IO ([String], IO ())

verbMsg

Arguments

:: Handle

Output handle

-> Int

global verbosity

-> Int

message level

-> String

message level

-> IO () 

Writes the message to the given handle unless the verbosity is less than the message level.

verbMsgLn :: Handle -> Int -> Int -> String -> IO ()

Same as verbMsg but with a newline at the end

verbMsgIO :: Int -> Int -> String -> IO ()

verbMsg with stdout as handle

verbMsgIOLn :: Int -> Int -> String -> IO ()

verbMsgLn with stdout as handle