{-# LANGUAGE LambdaCase #-}
module Drama.Loop
( loop
, continue
, stop
, forever
)
where
import Control.Monad (forever)
loop
:: Monad m
=> s
-> (s -> m (Either s a))
-> m a
loop :: s -> (s -> m (Either s a)) -> m a
loop s
s0 s -> m (Either s a)
k =
s -> m (Either s a)
k s
s0 m (Either s a) -> (Either s a -> m a) -> m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Left s
s -> s -> (s -> m (Either s a)) -> m a
forall (m :: * -> *) s a.
Monad m =>
s -> (s -> m (Either s a)) -> m a
loop s
s s -> m (Either s a)
k
Right a
x -> a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x
continue :: Monad m => s -> m (Either s a)
continue :: s -> m (Either s a)
continue s
s = Either s a -> m (Either s a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (s -> Either s a
forall a b. a -> Either a b
Left s
s)
stop :: Monad m => a -> m (Either s a)
stop :: a -> m (Either s a)
stop a
x = Either s a -> m (Either s a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Either s a
forall a b. b -> Either a b
Right a
x)