----------------------------------------------------------------------------- -- -- Module : Control.Monad.Util -- Copyright : (c) 2017 Brian W Bush -- License : MIT -- -- Maintainer : Brian W Bush -- Stability : Stable -- Portability : Portable -- -- | Utilities for monads. -- ----------------------------------------------------------------------------- module Control.Monad.Util ( -- * Iteration collectWhile ) where -- | Collect the results of running an action repeatedly while a condition holds. collectWhile :: Monad m => m a -- ^ The action to repeat. -> m Bool -- ^ The action for the condition that must hold. -> m [a] -- ^ An action collecting the results. collectWhile next test = do t <- test if t then do n <- next (n :) <$> collectWhile next test else return []