-- | Waiting (for replies).
module Sound.Osc.Wait where

-- * Wait

-- | Repeat action until predicate /f/ is 'True' when applied to result.
untilPredicate :: Monad m => (a -> Bool) -> m a -> m a
untilPredicate :: forall (m :: * -> *) a. Monad m => (a -> Bool) -> m a -> m a
untilPredicate a -> Bool
f m a
act =
    let g :: a -> m a
g a
p = if a -> Bool
f a
p then m a
recur else forall (m :: * -> *) a. Monad m => a -> m a
return a
p
        recur :: m a
recur = m a
act forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> m a
g
    in m a
recur

-- | Repeat action until /f/ does not give 'Nothing' when applied to result.
untilMaybe :: Monad m => (a -> Maybe b) -> m a -> m b
untilMaybe :: forall (m :: * -> *) a b. Monad m => (a -> Maybe b) -> m a -> m b
untilMaybe a -> Maybe b
f m a
act =
    let g :: a -> m b
g a
p = forall b a. b -> (a -> b) -> Maybe a -> b
maybe m b
recur forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe b
f a
p)
        recur :: m b
recur = m a
act forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> m b
g
    in m b
recur