In particular, functions for retrying a MonadPlus action: It is repeated until it returns a valid result.
Signed-off-by: Petr Pudlak <[email protected]> --- Makefile.am | 1 + src/Ganeti/Utils/MonadPlus.hs | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/Ganeti/Utils/MonadPlus.hs diff --git a/Makefile.am b/Makefile.am index f7c3486..774e0d9 100644 --- a/Makefile.am +++ b/Makefile.am @@ -845,6 +845,7 @@ HS_LIB_SRCS = \ src/Ganeti/Utils/Atomic.hs \ src/Ganeti/Utils/AsyncWorker.hs \ src/Ganeti/Utils/Livelock.hs \ + src/Ganeti/Utils/MonadPlus.hs \ src/Ganeti/VCluster.hs \ src/Ganeti/WConfd/ConfigState.hs \ src/Ganeti/WConfd/ConfigWriter.hs \ diff --git a/src/Ganeti/Utils/MonadPlus.hs b/src/Ganeti/Utils/MonadPlus.hs new file mode 100644 index 0000000..7ce2255 --- /dev/null +++ b/src/Ganeti/Utils/MonadPlus.hs @@ -0,0 +1,42 @@ +{-| Utility functions for MonadPlus operations + +-} + +{- + +Copyright (C) 2014 Google Inc. + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301, USA. + +-} + +module Ganeti.Utils.MonadPlus + ( mretryN + , retryMaybeN + ) where + +import Control.Monad +import Control.Monad.Trans.Maybe + +-- | Retries the given action up to @n@ times. +-- The action signals failure by 'mzero'. +mretryN :: (MonadPlus m) => Int -> (Int -> m a) -> m a +mretryN n = msum . flip map [1..n] + +-- | Retries the given action up to @n@ times. +-- The action signals failure by 'mzero'. +retryMaybeN :: (Monad m) => Int -> (Int -> MaybeT m a) -> m (Maybe a) +retryMaybeN = (runMaybeT .) . mretryN -- 1.9.1.423.g4596e3a
