Revision 72747d91 src/Ganeti/Utils.hs

b/src/Ganeti/Utils.hs
51 51
  , chompPrefix
52 52
  , wrap
53 53
  , trim
54
  , defaultHead
55
  , exitIfEmpty
54 56
  ) where
55 57

  
56 58
import Data.Char (toUpper, isAlphaNum, isDigit, isSpace)
......
369 371
-- strings.
370 372
trim :: String -> String
371 373
trim = reverse . dropWhile isSpace . reverse . dropWhile isSpace
374

  
375
-- | A safer head version, with a default value.
376
defaultHead :: a -> [a] -> a
377
defaultHead def []    = def
378
defaultHead _   (x:_) = x
379

  
380
-- | A 'head' version in the I/O monad, for validating parameters
381
-- without which we cannot continue.
382
exitIfEmpty :: String -> [a] -> IO a
383
exitIfEmpty _ (x:_) = return x
384
exitIfEmpty s []    = exitErr s

Also available in: Unified diff