module Yi.Monad (
gets,
getsAndModify,
maybeM,
repeatUntilM,
whenM,
with,
) where
import Control.Monad.Reader
import Control.Monad.State
import Control.Monad.Base
getsAndModify :: MonadState s m => (s -> (s,a)) -> m a
getsAndModify f = do
e <- get
let (e',result) = f e
put e'
return result
with :: (MonadReader r m, MonadBase b m) => (r -> a) -> (a -> b c) -> m c
with f g = do
yi <- ask
liftBase $ g (f yi)
whenM :: Monad m => m Bool -> m () -> m ()
whenM mtest ma = mtest >>= flip when ma
maybeM :: Monad m => (x -> m ()) -> Maybe x -> m ()
maybeM _ Nothing = return ()
maybeM f (Just x) = f x
repeatUntilM :: Monad m => m (Bool,a) -> m [a]
repeatUntilM m = do
(proceed,x) <- m
if proceed
then (do xs <- repeatUntilM m
return (x:xs))
else return [x]