module MonadHelp where

untilJust :: Monad m => m (Maybe a) -> m a
untilJust m = go
  where
    go = do
        mayx <- m
        maybe go return mayx

untilJustCount :: Monad m => m (Maybe a) -> m (a, Int)
untilJustCount m = go 0
  where
    go i = do
        mayx <- m
        case mayx of
            Nothing -> go (i+1)
            Just x  -> return (x,i)
