1 {-| Utility functions -}
3 module Ganeti.HTools.Utils where
7 import qualified Data.Version
13 import qualified Ganeti.HTools.Version as Version
17 -- | To be used only for debugging, breaks referential integrity.
18 debug :: Show a => a -> a
19 debug x = trace (show x) x
21 -- | Check if the given argument is Left something
22 isLeft :: Either a b -> Bool
28 fromLeft :: Either a b -> a
29 fromLeft = either (\x -> x) (\_ -> undefined)
31 fromRight :: Either a b -> b
32 fromRight = either (\_ -> undefined) id
34 -- | Comma-join a string list.
35 commaJoin :: [String] -> String
36 commaJoin = intercalate ","
38 -- | Split a string on a separator and return an array.
39 sepSplit :: Char -> String -> [String]
41 | x == "" && xs == [] = []
44 | otherwise = x:(sepSplit sep ys)
45 where (x, xs) = break (== sep) s
48 -- | Partial application of sepSplit to @'.'@
49 commaSplit :: String -> [String]
50 commaSplit = sepSplit ','
52 -- | Swap a list of @(a, b)@ into @(b, a)@
53 swapPairs :: [(a, b)] -> [(b, a)]
54 swapPairs = map (\ (a, b) -> (b, a))
56 -- Simple and slow statistical functions, please replace with better versions
58 -- | Mean value of a list.
59 meanValue :: Floating a => [a] -> a
60 meanValue lst = (sum lst) / (fromIntegral $ length lst)
62 -- | Standard deviation.
63 stdDev :: Floating a => [a] -> a
65 let mv = meanValue lst
66 square = (^ (2::Int)) -- silences "defaulting the constraint..."
67 av = sum $ map square $ map (\e -> e - mv) lst
68 bv = sqrt (av / (fromIntegral $ length lst))
72 -- | Coefficient of variation.
73 varianceCoeff :: Floating a => [a] -> a
74 varianceCoeff lst = (stdDev lst) / (fromIntegral $ length lst)
76 -- | Get a Right result or print the error and exit
77 readData :: (String -> IO (Either String String)) -> String -> IO String
82 putStrLn $ fromLeft nd
83 exitWith $ ExitFailure 1
86 showVersion :: String -- ^ The program name
87 -> String -- ^ The formatted version and other information data
89 printf "%s %s\ncompiled with %s %s\nrunning on %s %s\n"
91 compilerName (Data.Version.showVersion compilerVersion)