1 {-| Utility functions -}
3 module Ganeti.HTools.Utils
24 -- | To be used only for debugging, breaks referential integrity.
25 debug :: Show a => a -> a
26 debug x = trace (show x) x
28 -- | Check if the given argument is Left something
29 isLeft :: Either a b -> Bool
35 fromLeft :: Either a b -> a
36 fromLeft = either (\x -> x) (\_ -> undefined)
38 fromRight :: Either a b -> b
39 fromRight = either (\_ -> undefined) id
41 -- | Comma-join a string list.
42 commaJoin :: [String] -> String
43 commaJoin = intercalate ","
45 -- | Split a string on a separator and return an array.
46 sepSplit :: Char -> String -> [String]
48 | x == "" && xs == [] = []
51 | otherwise = x:(sepSplit sep ys)
52 where (x, xs) = break (== sep) s
55 -- | Partial application of sepSplit to @'.'@
56 commaSplit :: String -> [String]
57 commaSplit = sepSplit ','
59 -- | Swap a list of @(a, b)@ into @(b, a)@
60 swapPairs :: [(a, b)] -> [(b, a)]
61 swapPairs = map (\ (a, b) -> (b, a))
63 -- Simple and slow statistical functions, please replace with better versions
65 -- | Mean value of a list.
66 meanValue :: Floating a => [a] -> a
67 meanValue lst = (sum lst) / (fromIntegral $ length lst)
69 -- | Standard deviation.
70 stdDev :: Floating a => [a] -> a
72 let mv = meanValue lst
73 square = (^ (2::Int)) -- silences "defaulting the constraint..."
74 av = sum $ map square $ map (\e -> e - mv) lst
75 bv = sqrt (av / (fromIntegral $ length lst))
78 -- | Coefficient of variation.
79 varianceCoeff :: Floating a => [a] -> a
80 varianceCoeff lst = (stdDev lst) / (fromIntegral $ length lst)
82 -- | Get a Right result or print the error and exit
83 readData :: (String -> IO (Either String String)) -> String -> IO String
88 putStrLn $ fromLeft nd
89 exitWith $ ExitFailure 1