Commit bf7ee7ad authored by Klaus Aehlig's avatar Klaus Aehlig

Provide function to obtain the unique element of a list

This version of 'the' properly lives in the 'Result' monad,
as opposed to traditional one calling 'error'. The reason
why it is 'Bad' that not precisely one element is returned
is given as argument.
Signed-off-by: default avatarKlaus Aehlig <aehlig@google.com>
Reviewed-by: default avatarMichele Tartara <mtartara@google.com>
parent cef3f99f
......@@ -58,6 +58,7 @@ module Ganeti.Utils
, splitEithers
, recombineEithers
, resolveAddr
, monadicThe
, setOwnerAndGroupFromNames
, formatOrdinal
, atomicWriteFile
......@@ -423,6 +424,13 @@ exitIfEmpty :: String -> [a] -> IO a
exitIfEmpty _ (x:_) = return x
exitIfEmpty s [] = exitErr s
-- | Obtain the unique element of a list in an arbitrary monad.
monadicThe :: (Eq a, Monad m) => String -> [a] -> m a
monadicThe s [] = fail s
monadicThe s (x:xs)
| all (x ==) xs = return x
| otherwise = fail s
-- | Split an 'Either' list into two separate lists (containing the
-- 'Left' and 'Right' elements, plus a \"trail\" list that allows
-- recombination later.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment