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