On Fri, Nov 8, 2013 at 11:05 AM, Klaus Aehlig <[email protected]> wrote: > 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: Klaus Aehlig <[email protected]> > --- > src/Ganeti/Utils.hs | 8 ++++++++ > 1 file changed, 8 insertions(+) > > diff --git a/src/Ganeti/Utils.hs b/src/Ganeti/Utils.hs > index 5d5194c..5dcbe60 100644 > --- a/src/Ganeti/Utils.hs > +++ b/src/Ganeti/Utils.hs > @@ -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 only element of a list in the Result 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. > -- > 1.8.4.1 >
LGTM, thanks. Michele -- Google Germany GmbH Dienerstr. 12 80331 München Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg Geschäftsführer: Graham Law, Christine Elizabeth Flores
