raft-0.3.11.0: src/Control/Monad/Util.hs
-----------------------------------------------------------------------------
--
-- Module : Control.Monad.Util
-- Copyright : (c) 2017 Brian W Bush
-- License : MIT
--
-- Maintainer : Brian W Bush <consult@brianwbush.info>
-- 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 []