Add a function that iterates a function of type `a -> Maybe a`
in the sense of the Maybe monad until nothing is obtained. The
last Just value is not dropped. (This differs from the
semantics of iterateOk.)

Signed-off-by: Klaus Aehlig <[email protected]>
---
 src/Ganeti/Utils.hs | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/src/Ganeti/Utils.hs b/src/Ganeti/Utils.hs
index 1365440..fbe3a36 100644
--- a/src/Ganeti/Utils.hs
+++ b/src/Ganeti/Utils.hs
@@ -100,6 +100,7 @@ module Ganeti.Utils
   , maxBy
   , threadDelaySeconds
   , monotoneFind
+  , iterateJust
   ) where
 
 import Prelude ()
@@ -869,3 +870,8 @@ monotoneFind heuristics p xs =
              then Just x
              else monotoneFind heuristics p xs'
     _ -> Nothing
+
+-- | Iterate a funtion as long as it returns Just values, collecting
+-- all the Justs that where obtained.
+iterateJust :: (a -> Maybe a) -> a -> [a]
+iterateJust f a = a : maybe [] (iterateJust f) (f a)
-- 
2.6.0.rc2.230.g3dd15c0

Reply via email to